word 批量转 pdf 再合并

依赖包

pip install pywin32

pip install PyPDF2

import os
import win32com.client
from PyPDF2 import PdfFileMerger

source_dir = os.getcwd()

wdFormatPDF = 17

word = win32com.client.Dispatch('Word.Application')

for item in os.listdir(source_dir):
    if item.endswith(".docx") or item.endswith(".doc"):
        inputFile = os.path.join(source_dir,item)
        outputFile = os.path.join(source_dir,item.replace(".docx", ".pdf").replace(".doc", ".pdf"))
        doc = word.Documents.Open(inputFile)
        doc.SaveAs(outputFile, FileFormat=wdFormatPDF)
        
doc.Close()
word.Quit()

merger = PdfFileMerger()

for item in os.listdir(source_dir):
    if item.endswith(".pdf"):
        merger.append(item)
    
merger.write(os.path.join(source_dir,'000.pdf'))
merger.close()

打包

pip install pyinstaller

pyinstaller -F xxx.py

posted @ 2022-03-16 00:15  weichangk  阅读(121)  评论(0编辑  收藏  举报