Files
amazon-invoices/combine_pdfs.py
2026-03-23 20:25:30 -05:00

32 lines
821 B
Python

from PyPDF2 import PdfMerger, PdfReader
import os
# Directory containing the invoice PDFs
invoices_dir = "invoices"
# Get all PDF files and sort them
pdf_files = [f for f in os.listdir(invoices_dir) if f.endswith('.pdf')]
pdf_files.sort()
# Create a PDF merger object
merger = PdfMerger()
# Add each PDF to the merger
for pdf_file in pdf_files:
pdf_path = os.path.join(invoices_dir, pdf_file)
# For Audible PDFs, only include the first page
if "Audible" in pdf_file:
merger.append(pdf_path, pages=(0, 1))
print(f"Added (first page only): {pdf_file}")
else:
merger.append(pdf_path)
print(f"Added: {pdf_file}")
# Write the combined PDF
output_path = "receipts.pdf"
merger.write(output_path)
merger.close()
print(f"\nCombined {len(pdf_files)} PDFs into {output_path}")