How to copy text from a cell on Excel file to a PDF form with python?

65 Views Asked by At

I'm developing a Python script that reads a specific cell from an Excel file, say B6, and transcribes it to a text box on a PDF form, let's say the text box is called Formulary unite_3.

Python correctly reads the text inside the Excel cell, even prints it, and transfers it to the form, but when I open the pdf file it is not visible until I click on the file.

I already tried to fix the problems, I even made a mini script that allows you to see the properties of the text box to see the behavior, and what I deduce is that what you put in the form you put layers behind. I attach my analysis below:

With this, the text doesn't appear (done indirectly with Python):

{'/AP': {'/N': IndirectObject(113, 0)}, '/DA': '/Helv 0 Tf 0 g', '/DR': {'/Encoding': {'/PDFDocEncoding': IndirectObject(110, 0)}, '/Font': {'/Helv': IndirectObject(111, 0)}}, '/F': 4, '/FT': '/Tx', '/P': IndirectObject(106, 0), '/Rect': [120.84, 600.24, 293.28, 617.16], '/Subtype': '/Widget', '/T': 'Formulary unite_3', '/TU': 'First Name', '/Type': '/Annot', '/V': 'Hola Mundo'}

With this, the text appears (done directly with Adobe Acrobat Reader):

#{'/AP': {'/N': IndirectObject(48, 0)}, '/DA': '/Helv 0 Tf 0 g', '/DR': {'/Encoding': {'/PDFDocEncoding': IndirectObject(772, 0)}, '/Font': {'/Helv': IndirectObject(773, 0)}}, '/F': 4, '/FT': '/Tx', '/P': IndirectObject(1, 0), '/Rect': [120.84, 600.24, 293.28, 617.16], '/Subtype': '/Widget', '/T': 'Formulary unite_3', '/TU': 'First Name', '/Type': '/Annot', '/V': 'Hola Mundo'}

I have tried many libraries but nothing has changed. I am currently using PyPDF2 which has given me better results. I leave you my script and if someone could help me please, I am very desperate for this.

I purposely left out the Excel part because I could already get that to work, and replaced it with Hola Mundo for practicality purposes.

Python Script

Part of form where it should appear at a glance

Part of the form where it can only be viewed by clicking

1

There are 1 best solutions below

0
K J On

These questions asking why PDF software is having problems with "Forms" come up from time to time and the common reason is they are non conventional forms that should never have been given the file extension .PDF as they are XFA.XMP in a PDF wrapper.

enter image description here

Their behaviour depends on their design intent and should only be used in any program as they were intended. Usually are restricted by time (i.e. Expires 07/31/2024)

In this case as with many others this version is not designed for programming but anyone can attempt contravening the legal requirements. The form is legally to be entered by a responsible human having permitted the inbuilt JavaScript to run that adapts the contents as needed.

if (typeof(this.ADBE) == "undefined")
   this.ADBE = new Object();
ADBE.LANGUAGE = "ENU";
ADBE.Viewer_string_Title = "Adobe Acrobat DC";
ADBE.Viewer_string_Update_Desc = "Adobe Interactive Forms Update";
ADBE.Reader_string_Need_New_Version_Msg = "This PDF file requires a newer version of Adobe Acrobat Reader DC. Press OK to download the latest version or see your system administrator.";
ADBE.Viewer_string_Need_New_Version_Msg_Old = "This PDF requires a newer version of Acrobat. Copy this URL and paste into your browser or see your sys admin.";
ADBE.Viewer_string_Need_New_Version_Msg = "This PDF form requires a newer version of Adobe Acrobat DC. Without a newer version, the form may display, but may not work properly. Some form elements might not be visible at all. Click OK for more information on obtaining the latest version of Adobe Acrobat Reader DC.";
ADBE.Viewer_string_Need_New_Version_Msg_Updater = "This PDF form requires a newer version of Adobe Acrobat. Without a newer version, the form may display, but may not work properly. Some form elements might not be visible at all. If an internet connection is available, clicking OK will download and install the latest version.";

Thus you are warned it cannot work acceptably outside Adobe products

enter image description here However you can bypass that but technically you are contravening the requirements for USE which is clear it is designed as a paper printout which should have been filled by the applicant

enter image description here

NOTE: Read the Penalties section of the Form I-130
Instructions before completing this part.

Copies of any documents I have submitted are exact
photocopies of unaltered, original documents, and I understand
that USCIS may require that I submit original documents to
USCIS at a later date.