欢迎光临
我们一直在努力

python开发批量word转pdf(windows环境)

基于python开发的word转pdf工具,当前只支持windows系统

1.把所有word转化为PDF

from win32com.client import Dispatch
import os

pdfRoot = "D:\Desktop\wordToPDF\pdf" #保存pdf结果的文件夹
wordRoot = "D:\Desktop\wordToPDF\word" #读取word的文件夹

def doc2pdf(filePath, file):
    print("正在转换:",file)
    word = Dispatch('Word.Application')
    doc = word.Documents.Open(filePath)
    outFile = pdfRoot +"\\"+ file.split('.')[0] + ".pdf" #生成pdf文件路径名称
    doc.SaveAs(outFile, FileFormat=17)
    doc.Close()
    word.Quit()

if __name__ == "__main__":
    filelist = os.listdir(wordRoot)
    for file in filelist:
        if (file.endswith(".doc") or file.endswith(".docx")) and ("~$" not in file):
            filePath = wordRoot+"\\"+file
            doc2pdf(filePath, file)
    print ("所有word文件转PDF文件已完成!!!")

 

2.合并所有PDF文件

 

from PyPDF2 import PdfFileMerger
import os

pdfRoot = "D:\Desktop\wordToPDF\pdf" #保存pdf结果的文件夹
merger = PdfFileMerger() #调用PDF文件合并模块
filelist=os.listdir(pdfRoot) #读取文件夹所有文件
for file in filelist:
    if file.endswith(".pdf"):
        merger.append(pdfRoot+"\\"+file)#合并PDF文件
merger.write("result.pdf") #写入PDF文件

 

3.多个word文件合并

 

from os.path import abspath
from win32com import client
import os

wordRoot = "D:\Desktop\Ada\word" #读取word的文件夹
final_docx = r"D:\Desktop\Ada\result\word文件合并结果2.docx"
files = list()
filelist = os.listdir(wordRoot)
for file in filelist:
    if (file.endswith(".doc") or file.endswith(".docx")) and ("~$" not in file):
        filePath = wordRoot+"\\"+file
        files.append(filePath)
# 启动word应用程序
word = client.gencache.EnsureDispatch("Word.Application")
word.Visible = True
# 新建空白文档
new_document = word.Documents.Add()

for fn in files[::-1]:
    print ("正在合并:", fn)
    fn = abspath(fn)
    new_document.Application.Selection.Range.InsertFile(fn)
# 保存最终文件,关闭Word应用程序
new_document.SaveAs(final_docx)
new_document.Close()
word.Quit()

 

未经允许不得转载:小罗同学 » python开发批量word转pdf(windows环境)

分享到:更多 ()