7

I have a pdf file and I need to edit some text/values in the pdf. For example, In the pdf files that I have BIRTHDAY DD/MM/YYYY is always N/A. I want to change it to whatever value I desire and then save it as a new document. Overwriting existing document is also alright.

I have previously done this so far:

from PyPDF2 import PdfReader, PdfWriter

reader = PdfReader("abc.pdf")
page = reader.pages[0]

writer = PdfWriter()
writer.add_page(reader.pages[0])
pdf_doc = writer.update_page_form_field_values(
    reader.pages[0], {"BIRTHDAY DD/MM/YYYY": "123"}
)
with open("new_abc1.pdf", "wb") as fh:
    writer.write(fh)

But this update_page_form_field_values() doesn't change the desired value, maybe because this is not a form field?

Screenshot of pdf showing the value to be changed:

screenshot_image

Any clues?

Javad
  • 2,033
  • 3
  • 13
  • 23
rootkit
  • 353
  • 1
  • 3
  • 15

1 Answers1

0

I'm the current maintainer of pypdf and PyPDF2 (Please use pypdf; PyPDF2 is deprecated)

It is not possible to change a text with pypdf at the moment.

Changing form contents is a different story. However, we have several issues with form fields: https://github.com/py-pdf/pypdf/labels/workflow-forms

The update_page_form_field_values is the correct function to use.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958