comtypes加word 2013批量将pdf转换为doc
office 2013很强大.
import os
import sys
import re
import comtypes.client
wdFormatPDF = 17
def covx_to_pdf(infile, outfile):
"""Convert a Word .docx to PDF"""
print('making:',outfile)
word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(infile)
doc.SaveAs(outfile)
doc.Close()
word.Quit()
total = 0
for root,dirs,files in os.walk('.'):
for filespath in files:
# print(filespath)
p = os.path.abspath( os.path.join(root, filespath) )
zhen = re.search(r'(\w+镇)|(\w+乡)', filespath)
cun = re.search(r'((\w+镇)|(\w+乡))(\w+村)', filespath)
if zhen and cun:
print(p, zhen.groups(), cun.groups())
zhen = zhen.group(1) or zhen.group(2)
cun = cun.group(4)
outp = os.path.abspath( os.path.join(root, 'output/'+zhen+'/'+cun+'.doc') )
folder, fn = os.path.split(outp)
if not os.path.exists(folder) :
os.makedirs(folder)
if p[-3:] == 'pdf':
total += 1
covx_to_pdf(p, outp)
print('共生成',total)