在工作中经常会遇到将word文件转换为pdf文件的情况,如果文件很少的情况下,可以挨个打开并另存为pdf;如果是成百上千甚至更多的情况下,挨着另存就会耗费大量时间。幸运的是,利用python可以实现word转pdf的批量转换,还可以自定义保存路径以及修改前后缀,下面将简单介绍一个例子,功能不甚完善,缺点是只可以在windows操作系统下运行并且要求安装office套件。
一、首先需要安装pywin32模块。该模块包含了大量的Windows API,通过该模块,可以很方便地从python直接调用word,安装命令如下:
pip install pywin32
二:根据需求编写代码,下面示例有详细注释,可进行参考,根据自身需求进行修改:
from win32com.client import constants, gencache #导入win32com模块中相应函数,用来启动word
from os import walk
import os
from multiprocessing import Pool
#对word文件进行转换并保存
def d2p(input_file,newName):
try:
print("------"+input_file+"---Concert Start------")
word=gencache.EnsureDispatch('Word.Application')
wFile=word.Documents.Open(input_file,ReadOnly=1) #打开word
wFile.SaveAs(newName,FileFormat=17) #以pdf格式保存
word.Quit() #退出word
print("------"+newName+"---Concert Finished!!!------")
except:
print("Please make sure that the word application has been successfully installed!!!")
#获取所有pdf文件(pdf保存目录、脚本所在目录)
def getPFiles():
prePath=os.path.split(os.path.realpath(__file__))[0] #脚本所在目录
pathList=[prePath,pdfPath]
pFiles=[]
i=0
if not os.path.exists(pdfPath):
os.makedirs(pdfPath) #如果要保存pdf的目录不存在则进行创建
else:
while i<2:
for root,dirs,Files in walk(pathList[i]):
for file in Files:
if file.endswith(".pdf"):
pFiles.append(file)
else:
pass
i+=1
return pFiles
#获取脚本执行目录中所有word文件列表(不包含已存在pdf格式的word文件)
def getWFiles(pFiles):
"""
:param pFiles:所有的pdf文件(脚本所在目录的根目录下的pdf文件以及所要保存pdf文件的位置目录下的pdf文件)
"""
wFiles=[]
prePath=os.path.split(os.path.realpath(__file__))[0]
for root,dirs,files in walk(prePath):
for file in files:
if file.endswith('doc') or file.endswith('docx'):
if (file.split('.')[0]+'.pdf') not in pFiles:
wFiles.append(os.path.join(root,file))
else:
pass
else:
pass
return list(set(wFiles))
#获取所有的word文件路径以及pdf文件路径
def resolvePath(pFiles):
"""
:param pFiles:所有的pdf文件(脚本所在目录的根目录下的pdf文件以及所要保存pdf文件的位置目录下的pdf文件)
"""
pAbPath=[]
wAbPath=getWFiles(pFiles)
i=0
while i
仅以此作为学习笔记以及分享,如有需改进或者不妥之处,请多多指教。
1+0.01=1.01
1-0.01=0.99
