Search code examples
pythonpdfvisual-studio-codepypdf

Python PyPDF2 'PdfFileReader' object has no attribute 'scaleTo' error


This is my first program so I imagine there are a lot of inefficiencies. First I created a GUI that works on a combined PDF. In attempting to convert the working code to a code that iterates through a directory of multiple single page PDF's, I get an error. On the "PageObj.scaleTo(1172, 1772)" line I get the error in the question title. A GUI takes the user inputs for the variables "x" (directory), "a" (paper size), and "s" (state). It is to resize the page to the selected size, merge with a template (not append but a single page "PDF sandwich" I have heard it described), then overwrite the existing file. This is to happen to every PDF in the specified directory. I have tried several version of defining my PageObj variable, but can't seem to get it right.

# Variables for User input values
x = values["-pdf_dir-"]
a = values["-paper_size-"]
s = values["-state-"]

# Location to find seal templates
state = f"G:/Drafting/Kain Mincey/Allen's seals/Correctly Sized/{a}/{s}.pdf"

Seal_pdf = PdfFileReader(open(state, "rb"), strict=False)
input_pdf = glob.glob(os.path.join(x, '*.pdf'))
output_pdf = PdfFileWriter()
page_count = len(fnmatch.filter(os.listdir(x), '*.pdf'))
i = 0

if a == "11x17":
    for file in input_pdf:
        sg.OneLineProgressMeter('My Meter', i, page_count, 'And now we Wait.....')
        PageObj = PyPDF2.PdfFileReader(open(file, "rb"))
        PageObj.scaleTo(11*72, 17*72)
        PageObj.mergePage(Seal_pdf.getPage(0))
        output_pdf.addPage(PageObj)
        output_filename = f"{x[:-4]}"
        i = i + 1

Solution

  • PdfFileReader returns the whole file. scaleTo applies to a page. You have to fetch the page you want with getPage. –Tim Roberts Mar 28 at 21:02