'Fill PDF form values using PyPDF2 multiple pages but getting same and duplicate data on all pages of pdf

I have used this below code.

from PyPDF2 import PdfFileWriter, PdfFileReader
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject


def set_need_appearances_writer(writer: PdfFileWriter):
    # See 12.7.2 and 7.7.2 for more information:
    # http://www.adobe.com/content/dam/acom/en/devnet/acrobat/
    #   pdfs/PDF32000_2008.pdf
    try:
        catalog = writer._root_object
        # get the AcroForm tree
        if "/AcroForm" not in catalog:
            writer._root_object.update(
                {
                    NameObject("/AcroForm"): IndirectObject(
                        len(writer._objects), 0, writer
                    )
                }
            )

        need_appearances = NameObject("/NeedAppearances")
        writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
        # del writer._root_object["/AcroForm"]['NeedAppearances']
        return writer

    except Exception as e:
        print("set_need_appearances_writer() catch : ", repr(e))
        return writer


list_data = [
    {"7014": "1", "Datframst": "2022-04-05"},
    {"7014": "2", "Datframst": "2022-04-05"},
]

myfile = PdfFileReader("template/test.pdf")

writer = PdfFileWriter()
set_need_appearances_writer(writer)


for dict_data in list_data:
    for count in range(myfile.numPages):
        writer.updatePageFormFieldValues(
            myfile.getPage(count),
            fields=dict_data
        )
        writer.addPage(myfile.getPage(count))


with open("newfile.pdf", "wb") as new:
    writer.write(new)

list_data in the for loop has multiple dict. What this code does is create a file with multiple pages which I want but all the pages gets overwriiten by the last dict value (dict_data). When I do this with just one dict data the file with two pages of myfile varible has no problem. But when I use multiple dicts then the problem arises of all pages having same values. Please Help!

You can get the file here.



Sources

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

Source: Stack Overflow

Solution Source