2025年7月21日 星期一

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

系列文章:
3. 只要點兩下,就能將一堆的Doc與Docx 轉成 PDF
3.https://skjhcreator.blogspot.com/2023/05/docdocx-pdf.html
4.https://skjhcreator.blogspot.com/2023/06/jpgpdfjpgpdf.html
5.只要點兩下,就能將放進input的一堆PDF轉成各自的WORD
5.https://skjhcreator.blogspot.com/2023/10/inputpdfword.html
6.只要點兩下,就能將放進input的一堆PDF轉成在ouput資料夾內的各自的WORD 
6.https://skjhcreator.blogspot.com/2023/10/inputpdfouputword.html

7.只要點兩下,就能夠將InputAndOutput資料夾底下的子子孫孫資料夾內所有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合併 完成!!")  

資料來源:

沒有留言:

張貼留言

只要點兩下兩次,就能依照設定拆分指定PDF頁數並合併成設定好的一個PDF

        最近遇到一個問題,想要對四個不同的PDF,分別擷取1~2頁、3~4頁、5~6頁、7~8頁,合併成一個總共8頁的PDF。那該怎麼寫程式來處理這件事呢?那如果是用線上拆分PDF網頁與線上合併PDF網頁,該怎麼做呢?則是需要將四個不同PDF分別上傳到拆分網頁後再 一頁一...