引用: 批量转换word为pdf
程序如下:
import os,shutil
from win32com import client
def doc2pdf(doc_name, pdf_name):
"""
:word文件转pdf
:param doc_name word文件名称
:param pdf_name 转换后pdf文件名称
"""
try:
word = client.DispatchEx("Word.Application")
if os.path.exists(pdf_name):
os.remove(pdf_name)
worddoc = word.Documents.Open(doc_name,ReadOnly = 1)
worddoc.SaveAs(pdf_name, FileFormat = 17)
return pdf_name
except Exception as e:
print(e)
return 1
finally:
worddoc.Close()
word.Quit()
def doc2docx(doc_name,docx_name):
"""
:doc转docx
"""
try:
# 首先将doc转换成docx
word = client.Dispatch("Word.Application")
doc = word.Documents.Open(doc_name)
#使用参数16表示将doc转换成docx
doc.SaveAs(docx_name,16)
except:
pass
finally:
doc.Close()
word.Quit()
def createDirs(basePath=os.getcwd()):
# 存放转化后的pdf文件夹
pdfs_dir = basePath + '/pdfs'
if not os.path.exists(pdfs_dir):
os.mkdir(pdfs_dir)
return pdfs_dir
def getFileNames(basePath=os.getcwd()):
filenames=[]
# move all .words files to words_dir
for file in os.listdir(basePath):
if file.endswith('.docx'):
filenames.append(file)
elif file.endswith('.doc'):
filenames.append(file)
else:
pass
return filenames
def convert(basePath=os.getcwd(),filenames=[]):
pdfs_dir=createDirs(basePath)
for filename in filenames:
pdfName='.'.join(filename.split('.')[:-1])+'.pdf'
doc2pdf(os.path.join(basePath,filename),os.path.join(pdfs_dir,pdfName))
if __name__ == '__main__':
basePath=r"document directory here"
lfileNames=getFileNames(basePath)
print('are you going to convert these files to pdf?')
for filename in lfileNames:
print(filename)
print("yes/no?")
while True:
command=input()
if command=='yes':
convert(basePath,lfileNames)
break
elif command=='no':
break
else:
print('wrong command,input yes or no please')
注意
安装 win32com 模块时,使用命令 python -m pip install pypiwin32
来安装。 参考