본문 바로가기
ICT

[Python] Editing PDF Fillable Form

by NeoSailer 2023. 6. 15.

[Requirements]

The business receives a bunch of requests from the employees selling company stocks. However, it is hard for the employees to fill out the form due to language barrier and complexibility.

- Develop an automation process to fill out stock selling fillable PDF form

 

[OS] 

Windows 10

 

[Development Language]

Python

 

[IDE]

Pycham

 

[Prerequisites]

Install Python modules

- pdfrw

- fillpdfs

 

On command prompt window,

pip install pdfrw
pip install pip install fillpdfs

 

[Code]

## Getting annotations from the fillable PDF ##

import pdfrw

ANNOT_KEY = "/Annots"
ANNOT_FIELD_KEY = "/T"
ANNOT_VAL_KEY = "/V"
SUBTYPE_KEY = "/Subtype"
WIDGET_SUBTYPE_KEY = "/Widget"

PDF_NAME = "test.pdf"

template_pdf = pdfrw.PdfReader(PDF_NAME)
for page in range(0, len(template_pdf.pages)):
    annotations = template_pdf.pages[page][ANNOT_KEY]
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                name = annotation[ANNOT_FIELD_KEY]
                print("{} ".format(name), end="")
                if annotation[ANNOT_VAL_KEY]:
                    value = annotation[ANNOT_VAL_KEY]
                    print("= {}".format(value))
                else:
                    print()
                    

...
(NameTitle) 
(NameFirst) 
(NameSurname) 
(Homeaddress1) 
(Homeaddress2) 
...

Once get the all annotation, set a dictionary to fill out the PDF form

 

## Filling out the fillable PDF form ##

from fillpdf import fillpdfs

ANNOT_KEY = "/Annots"
ANNOT_FIELD_KEY = "/T"
ANNOT_VAL_KEY = "/V"
SUBTYPE_KEY = "/Subtype"
WIDGET_SUBTYPE_KEY = "/Widget"

PDF_NAME = "test_jh.pdf"
fillpdfs.get_form_fields(PDF_NAME)

data_dict = {
    'NameTitle': 'MR',
    'NameFirst': 'Jaehui',
    'NameSurname': 'Yoon',
    'Homeaddress1': '203-1703 Hangaram-maeul 2 dangi, Gurae-dong, Gimpo, South Korea',
    'PostCode': '10071',
    'Dateofbirth': '04 OCTOBER 1985',
    'Countryofbirth': 'SOUTH KOREA',
    'Townofbirth': 'BUSAN',
    'Mobile': '82 10 3108 9839',
    'Email': 'newind2000@hotmail.com',
    'CurrentEmployeeCheckYes': 'Yes',
    #'CurrentEmployeeCheckNo': 'Yes',
    'LeavingDate': '10 OCTOBER 2024',
    'ApplicationForeigner': 'FOREIGNER',
    'UKNationaNoCheck': 'Yes',
    'Sell': '100',
    'CompanyName': 'ROTORK PLC',
    'Tick': 'Yes',
    'BankName': 'KAKAOBANK CORP',
    'AccountHolderName': 'YOON JAEHEE',
    'BankAddress1': '11F, 131, Bundangnaegok-ro, Bundang-gu, Seongnam-si, Gyeonggi-do, 13529, Republic of Korea',
    'AccountNumber': '3333-08-5621079',
    'Text10': 'KAKOKR22XXX',
    'ComboBox13': 'GBP',
    'PrintName': 'JAEHUI YOON',
    'AccountDate': '10 OCTOBER 2024',
    'NationalFirst': 'JAEHEE',
    'NationaLast': 'YOON',
    'NationaD1': '0',
    'NationaD2': '4',
    'NationaM1': '1',
    'NationaM2': '0',
    'NationaY1': '1',
    'NationaY2': '9',
    'NationaY3': '8',
    'NationaY4': '5',
    'Countyr': 'REPUBLIC OF KOREA',
    'NationalIdentifier': 'PASSPORT1234',
    'NationaCheck1': 'X',
    'NationCheck2': 'X',
    'Nationa2D1': '1',
    'Nationa2D2': '0',
    'Nationa2M1': '1',
    'Nationa2M2': '0',
    'Nationa2Y1': '2',
    'Nationa2Y2': '0',
    'Nationa2Y3': '2',
    'Nationa2Y4': '4',
    'CrestCompany': 'ROTORK PLC',
    'ComboBox33': 'ORDINARY 0.5P',
    'CrestShare1': 'ONE HUNDRED',
    'CrestShare2': '100',
    'CrestShareName': 'JAEHUI YOON'
}
fillpdfs.write_fillable_pdf('test.pdf', 'newjh.pdf', data_dict)
fillpdfs.flatten_pdf('newjh.pdf', 'new_flat.pdf')

 

[Test]

Open the fillable PDF form and ensure that no information is filled.

Run the Python script(filing out the form) and check the PDF output

C:\Python310\python.exe C:\SRE\StockSelling\Test.py 

Process finished with exit code 0

 

[Lesson Learned]

- Pip install comman should be ran in Windows command prompt not in Python prompt

- Python is AWESOME

반응형

댓글