from win32com import client
import os
def doc2pdf(fn):
word = client.Dispatch("Word.Application") # open word
doc = word.Documents.Open(fn) # open word file
doc.SaveAs("{}.pdf".format(fn[:-4]), 17) # 17 means pdf file, save as pdf
doc.Close() # close word
word.Quit()
def docx2pdf(fn):
word = client.Dispatch("Word.Application") # open word
doc = word.Documents.Open(fn) # open word file
doc.SaveAs("{}.pdf".format(fn[:-5]), 17) # save as pdf 17means pdf file
doc.Close() # close word file
word.Quit()
path=r'C:\Users\Administrator\Desktop\xxx' # file path
files=[]
for file in os.listdir(path):
if file.endswith(".docx"): #only get word documents
files.append(path +"\\"+ file)
for d in files:
docx2pdf(d) #for docx documents
doc2pdf(d)#for doc documents
python批量转化word为PDF
于 2022-11-08 11:05:07 首次发布