安装win32com对应的控件
$ pip install pywin32 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
转换代码
# encoding=utf-8
import os
import traceback
import win32com
from win32com.client import constants
def transfer():
root = os.getcwd()
for parent, dirs, fs in os.walk(root):
for f in fs:
suffix = ".docx"
if f.endswith(suffix):
prefix = f[0:f.find(suffix)]
nName = prefix + ".pdf"
oldPath = os.path.sep.join((parent, f))
newPath = os.path.sep.join((parent, nName))
w = win32com.client.DispatchEx('Word.Application')
try:
doc = w.Documents.Open(oldPath, ReadOnly=1)
doc.ExportAsFixedFormat(newPath,
constants.wdExportFormatP