'Edit text in PDF with python

I have a pdf file and I need to edit some text/values in the pdf. For example, in the pdfs 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 PdfFileReader, PdfFileWriter

reader = PdfFileReader("abc.pdf")
page = reader.getPage(0)

writer = PdfFileWriter()
writer.addPage(reader.getPage(0))
pdf_doc = writer.updatePageFormFieldValues(
    reader.getPage(0), {"BIRTHDAY DD/MM/YYYY": "123"}
)
with open("new_abc1.pdf", "wb") as fh:
    writer.write(fh)

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

screenshot of pdf showing the value to be changed

Any clues?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source