Introduction
帳單合併是一種工作流程,使用 GroupDocs.Merger for Python 將多個財務文件合併成單一 PDF。財務團隊常需要將發票與送貨單、服務報告、保固條款一起寄送,但手動拼接檔案會產生錯誤且浪費時間。GroupDocs.Merger 可自動化此過程,確保每個附件正確排序、可選擇加密,並以一個乾淨的 PDF 交付。
在本教學中您將學會:
- 將純 PDF 發票與任意數量的輔助檔案合併。
- 處理 JPG 圖片與 DOCX 合約等混合格式。
- 開啟受密碼保護的發票、加入附件,並重新套用加密。
- 從大型附件中僅選取特定頁面,以保持最終檔案的精簡。
我在上個季度正好遇到這種情況:我們的會計系統產生了受密碼保護的發票,法律團隊又加入了多頁的保固 DOCX。以下程式碼在不到五分鐘內解決了問題。
When does it make sense to merge a password‑protected invoice with other documents?
Many enterprises encrypt invoices at rest to meet compliance requirements. When the invoice needs to be sent to a customer along with unprotected attachments (receipts, terms, images), the original password must be removed for merging and then reapplied to the final bundle. GroupDocs.Merger lets you supply the unlock password via LoadOptions, join the extra files, and finally protect the merged PDF with AddPasswordOptions. This keeps the confidentiality posture intact while delivering a single, easy‑to‑open document.
前置條件
- Python 3.8+(透過 .NET 橋接)
- GroupDocs.Merger for Python – 以以下指令安裝:
pip install groupdocs-merger
- 一份主要的發票 PDF(
invoice.pdf)以及附件檔案路徑清單。 - (可選)來源發票與輸出 PDF 的密碼。
步驟 1:PDF 附件的基本合併
最簡單的情況是將 PDF 發票與其他 PDF、影像或 Word 檔案合併。API 會自動在合併前將非 PDF 輸入轉換為 PDF。
import groupdocs_merger as gm
# Paths to source files
invoice = "invoice.pdf"
attachments = ["delivery_note.pdf", "service_report.pdf"]
output = "billing_package.pdf"
with gm.Merger(invoice) as merger:
for path in attachments:
merger.join(path) # Normalises each file to PDF and appends it
merger.save(output)
重點說明:
gm.Merger以主文件(發票)為參數建立實例。join()接受任何支援的格式;函式庫會自行處理轉換。save()將合併後的 PDF 寫入指定路徑。
步驟 2:合併混合格式附件
實務上的帳單包常包含掃描影像(JPG)與法律合約(DOCX)。相同程式碼即可運作,因為 GroupDocs.Merger 會即時將每個附件正規化為 PDF。
attachments = ["receipt.jpg", "warranty_terms.docx", "extra_clause.pdf"]
output = "billing_mixed.pdf"
with gm.Merger(invoice) as merger:
for path in attachments:
merger.join(path) # JPG and DOCX are converted to PDF automatically
merger.save(output)
為什麼重要: 將所有內容轉為 PDF 可免除收件人切換多種檢視器的需求,提供無縫的使用體驗。
步驟 3:處理受密碼保護的發票
若發票已加密,請透過 LoadOptions 提供密碼。合併完成後,可使用 AddPasswordOptions 為最終 PDF 重新設定密碼。
import io
invoice_password = "Inv$2026"
output_password = "Bill$2026"
load_options = gm.domain.options.LoadOptions(invoice_password)
buffer = io.BytesIO()
# Open the protected invoice, join attachments, write to memory buffer
with gm.Merger(invoice, load_options) as merger:
for path in attachments:
merger.join(path)
merger.save(buffer)
# Re‑secure the merged document
add_pwd = gm.domain.options.AddPasswordOptions(output_password)
buffer.seek(0)
with gm.Merger(buffer) as merger:
merger.add_password(add_pwd)
merger.save(output)
重點說明:
LoadOptions用於解鎖來源 PDF。- 合併結果先寫入記憶體串流(
BytesIO)。 AddPasswordOptions為最終檔案套用新密碼。
步驟 4:從附件中選取特定頁面
有時只需要附件中的部分頁面(例如長篇服務報告的前兩頁)。使用 PageJoinOptions 指定包含的 1 基索引頁碼範圍。
page_picks = [
("service_report.pdf", 1, 2), # Include pages 1‑2 only
("terms_and_conditions.pdf", 3, 3) # Include only page 3
]
output = "billing_selected_pages.pdf"
with gm.Merger(invoice) as merger:
for path, first, last in page_picks:
options = gm.domain.options.PageJoinOptions(first, last)
merger.join(path, options)
merger.save(output)
結果: 最終 PDF 包含發票以及僅選取的頁面,保持檔案輕量。
完整範例
完整可執行範例(含所有來源檔案與示範文件)已上傳至 GitHub:
billing-consolidator-demo-using-groupdocs-merger-python。
最佳實踐與提示
- 效能: 大批次處理時,只啟用必要的選項(例如避免不必要的格式轉換)。
- 記憶體管理: 始終使用
with陳述式,以確保 Mer