如何用Python实现doc文件批量转换为docx
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用Python实现doc文件批量转换为docx相关的知识,希望对你有一定的参考价值。
参考技术A
首先就是安装python之后的环境变量设置
然后其实直接安装了VS code再在扩展那安Python就能正常使用了。
在VS code调试python要用到交互的话,就要参考其他答案中使用“intergrated terminal /console”这个。
不过我正是恰恰卡在这一步。因为我不能运行debug。本回答被提问者采纳
python 使用LibreOffice的命令行界面将PDF文件转换为与Microsoft Office Word兼容的doc / docx文件。
#!C:/Python27/python.exe
#
# Convert PDF files to Microsoft Office Word compatible doc/docx files,
# using LibreOffice's command line interface.
#
# http://stackoverflow.com/questions/26358281/convert-pdf-to-doc-python-bash
# http://ask.libreoffice.org/en/question/20111/converting-files-using-soffice-convert-to-with-embedded-images-html-to-doc/
# http://cgit.freedesktop.org/libreoffice/core/tree/filter/source/config/fragments/filters
#
import os
import sys
import subprocess
# pdf source file(s) and target paths
basedir = 'C:/path/to'
pdfdir = os.path.normpath(basedir + '/pdf')
docdir = os.path.normpath(basedir + '/doc')
docxdir = os.path.normpath(basedir + '/docx')
# absolute path to libre office writer application
lowriter = 'C:/Progra~2/LibreO~1/program/swriter.exe'
# output-filter for conversion
#outfilter = ':"Office Open XML Text"'
#outfilter = ':"MS Word 2003 XML"'
#outfilter = ':"MS Word 2007 XML"'
#outfilter = ':"MS Word 97"'
outfilter = ''
i = 0
for top, dirs, files in os.walk(pdfdir):
for filename in files:
if filename.endswith('.pdf'):
i = i + 1
abspath_pdf = os.path.normpath(os.path.join(top, filename))
print 'Converting {0} into .doc format..'.format(abspath_pdf)
subprocess.call('{0} --invisible --convert-to doc{1} --outdir "{2}" "{3}"'
.format(lowriter, outfilter, docdir, abspath_pdf), shell=True)
print 'Converting {0} into .docx format..'.format(abspath_pdf)
subprocess.call('{0} --invisible --convert-to docx{1} --outdir "{2}" "{3}"'
.format(lowriter, outfilter, docxdir, abspath_pdf), shell=True)
print '|-------------------------------------------------------|'
print 'Done. Converted {0} pdf files.'.format(i)
以上是关于如何用Python实现doc文件批量转换为docx的主要内容,如果未能解决你的问题,请参考以下文章
用python实现批量替换.doc文件文件内容
如何将*.doc批量转换成*.docx
如何把doc批量转化为docx?
如何用纯java代码实现word转pdf?
python合并word
如何用纯java代码实现word转pdf