Search code examples
pythonpdfpypdf

Append a one page pdf to the end of all pdfs in a directory - python


I'm trying to add a 1 page pdf (lastpage) to the end of all invoice pdfs in a directory then rename the pdf as newname based on the filestart ('ICO_' + HH Name).

Issue 1.) My code is summing the previous invoices on top of the 1 page (1 = 1 + last, 2 = 1 + 2 + last, etc...). I've tried clearing filename (filename = "") before the rowcount = rowcount + 1 but that also didn't help.

Issue 2.) When the filestart encounters a similar last name (smitht and smithr) I will get an error that the file already exists when I know for a fact it is a different invoice. Is there a way to fix this as well?

import os
from openpyxl import load_workbook
from PyPDF2 import PdfFileMerger, PdfFileReader

book = load_workbook("/Invoice Names.xlsx")
ws = book.get_active_sheet()

lastpage = "/Invoice Last Page.pdf"
merger = PdfFileMerger()

rowcount = 2
for r in ws.rows:
    filestart ='ICO_' + ws.cell(row = rowcount, column = 1).value
    newname = ws.cell(row = rowcount, column = 2).value  
    for filename in os.listdir("invoices directory"):
        if filename.startswith(filestart):
            merger.append(PdfFileReader(file(filename, 'rb')))
            merger.append(PdfFileReader(file(lastpage, 'rb')))
            merger.write(newname)
            rowcount = rowcount + 1

Solution

  • 1) create a new instannce of merger for every merge (i.e. move it inside the loop):

    ...
    for r in ws.rows:
        merger = PdfFileMerger()
    

    2) It is hard to say without exact steps to reproduce