Francesco Anastasio
Francesco Anastasio

Reputation: 1

My script works alone, but doesn't as a function, why?

Good evening,

I am probably missing something very silly, that is making my project going much slower than I planned.

I wrote this script, whose function is to write a little block of formatted text with a specific content (name) on an existing PDF, then it should overwrite the input file with the new watermarked output.

The overwriting part gave me some headaches, so I just skipped it and set it to create a double copy with a different name. Here's the standalone script:

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4

## TEST LINE
name = 'PIANO'
page = '0.pdf'

# Create new file (stamp)
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=A4)
can.setFillColorRGB(0, 0, 0)
can.setFont('Helvetica', 8)
pageWidth, pageHeight = A4
can.drawString(pageWidth - 35, pageHeight - 20, f'{name}')
can.save()

# Move to the begin of StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)

# Reading input document
first_page = PdfFileReader(open(page, 'rb'), strict=False)
output = PdfFileWriter()

# Adding watermark and saving it to <NAME>_stamped.pdf
page = first_page.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)

with open(f'{name}_stamped.pdf', 'wb') as outputStream:
    output.write(outputStream)

As you see, it's quite straightforward.

When I try to implement this draft into my project as a function, I do this way:

def coverize(page: str, name: str):
    from PyPDF2 import PdfFileWriter, PdfFileReader
    import io
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import A4

    # Create new file (stamp)
    packet = io.BytesIO()
    can = canvas.Canvas(packet, pagesize=A4)
    can.setFillColorRGB(0, 0, 0)
    can.setFont('Helvetica', 8)
    pageWidth, pageHeight = A4
    can.drawString(pageWidth - 35, pageHeight - 20, f'{name}')
    can.save()

    # Move to the begin of StringIO buffer
    packet.seek(0)
    new_pdf = PdfFileReader(packet)

    # Reading first page
    first_page = PdfFileReader(open(page, 'rb'), strict=False)
    output = PdfFileWriter()

    # Adding watermark and saving it to <NAME>_stamped.pdf
    page = first_page.getPage(0)
    page.mergePage(new_pdf.getPage(0))
    output.addPage(page)

    with open(page, 'wb') as outputStream:
        output.write(outputStream)

I try to run the function with these parameters:

DEBUGLINE - page:     c:\Users\francesco\Desktop\PYTHON\Reporter3\files_to_merge\1.pdf #type string
DEBUGLINE - name:     GM #type string

But all i get is this output:

Traceback (most recent call last):
  File "c:\Users\francesco\Desktop\PYTHON\Reporter3\launcher.py", line 7, in <module>
    askAndRun(isFirstTime)
  File "c:\Users\francesco\Desktop\PYTHON\Reporter3\view_controller.py", line 104, in askAndRun
    run_program(usr_choice)
  File "c:\Users\francesco\Desktop\PYTHON\Reporter3\view_controller.py", line 48, in run_program
    print_report(report, missing)
  File "c:\Users\francesco\Desktop\PYTHON\Reporter3\pdf_manager.py", line 54, in print_report
    coverize(local_strings[0], name)
  File "c:\Users\francesco\Desktop\PYTHON\Reporter3\coverize.py", line 41, in coverize
    with open(page, 'wb') as outputStream:
TypeError: expected str, bytes or os.PathLike object, not PageObject

I tried to debug this with print statements, backtracing, everything I could, but i really don't understand WHY it passes a PageObject (which I guess comes from the PDF library) and NOT the string with the file path, while in the standalone script does.

It really seems like an error in the logic of the whole program to me.

I wish my question could be more specific, but I really can't figure out what the problem is, I hope someone may help me.

Upvotes: 0

Views: 49

Answers (1)

Jasmijn
Jasmijn

Reputation: 10452

You changed f'{name}_stamped.pdf' to page. If you change it back, it should work.

Upvotes: 1

Related Questions