2025年7月21日 星期一

只要點兩下,就可以將資料夾input內的所有Word通通轉成一個PDF


     最近在行政處室內聽聞,常常要將很多的Word轉成一個個相對應的PDF,再將一個個對應的PDF合併成一個PDF。而那個PDF就是要交給上級的檔案。特別是要送課程計畫時候,常常會忙到忘了到底是哪個Word轉到哪個PDF?合併後的PDF會不會少哪個PDF?聽到這些聲音,就開始思考能不能將這些步驟省略,直接將很多Word轉一個PDF?
        Recently, I heard in the administrative office that there is often a need to convert many Word documents into corresponding PDFs, and then merge those PDFs into a single PDF. This final PDF is required to be submitted to superiors. Especially when submitting course plans, it often gets so busy that one forgets which Word document corresponds to which PDF, and whether any PDFs are missing after merging. Hearing these concerns, I started to think about whether it is possible to skip these steps and directly convert multiple Word documents into a single PDF.

下載檔案解壓密碼:demo1234
Here is the website where you can download the program and find instructions:
Download。Extraction Password: demo1234
使用教學(Instructional videos):

以下是開發過程與原始碼 (Development process and code):
 
安裝套件pypiwin32 、 PyPDF2 
指令(command):
pip install pypiwin32
pip install PyPDF2
 
程式名稱(Program name):AllWord2OnePDF.py
程式內容(Code):

import glob
import os
from win32com import client
import time
from PyPDF2 import PdfFileMerger

# Word轉PDF
path=os.getcwd()
os.chdir(path+'\\input\\')
word = client.Dispatch("Word.Application")

for i in glob.glob('*.doc'):
    doc = word.Documents.Open(path+'\\input\\'+i)
    doc.SaveAs("{}.pdf".format(path+'\\output\\'+i[:-4]),17)
    doc.Close()
for i in glob.glob('*.docx'):
    docx = word.Documents.Open(path+'\\input\\'+i)
    docx.SaveAs("{}.pdf".format(path+'\\output\\'+i[:-5]),17)
    docx.Close()

word.Quit()
print("WORD轉PDF 完成!!")
# PDF合併
pdf_lst = list()
# 設定Input為目標路徑
os.chdir(path+'\\output')
target_path=str(os.path.abspath(os.getcwd()))
# 列出目標路徑內的pdf
for f in os.listdir(target_path):
    if f.endswith('.pdf'):
        pdf_lst.append(os.path.join(target_path,f))

# pdf 合併
file_merger = PdfFileMerger()
for pdf in pdf_lst:
    file_merger.append(pdf)

output_name =path+"\\"+"merge"+str(time.strftime("%H-%M-%S",time.localtime()))+".pdf"
file_merger.write(output_name)
print("PDF合併 完成!!")  

資料來源:

只要點兩下,就可以將資料夾input內的所有Word通通轉成一個PDF

  系列文章: 1.  只要點兩下,就能將一堆的Doc與Docx 轉成 PDF 1. https://skjhcreator.blogspot.com/2023/05/docdocx-pdf.html 2. 只要點兩下,就能將一堆的JPG轉成一個PDF,並以JPG所在的資料夾名稱...