1

Preface

System OS: Windows 10

First of, I have python in PATH, so that is not the problem. Other scripts work fine from the console.


Purpose of the python script?

Read parameters from a .csv file, and fill respective annotation fields in a .PDF


When the script is executed in Python's IDLE, the output pdf file is created. However, the pdf file is not created when the script is opened from the console.

I think it must have something to do with the Current Working Directory and my relative paths. That's why I added the line:

os.chdir(os.getcwd().replace(os.sep, '/'))

However, that did not seem to help.

Full Python script:

#! /usr/bin/python
import os , pdfrw , csv

INPUT_CSV_PATH = 'Tested_parameters.csv'
INVOICE_TEMPLATE_PATH = 'Input_template.pdf'
INVOICE_OUTPUT_PATH = 'output_document.pdf'
ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_FIELD_NAME = '/TU'
ANNOT_VAL_KEY = '/V'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'

def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    annotations = template_pdf.pages[1][ANNOT_KEY]
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY: # '/Subtype': '/Widget'
            if annotation[ANNOT_FIELD_KEY]: # '/T'
                key = annotation[ANNOT_FIELD_KEY][1:-1]
                if key in data_dict.keys():
                    if (key[0:4] == 'check'):
                        annotation.update( pdfrw.PdfDict( V=data_dict[key], AS=data_dict[key]) )        
                    else:
                        annotation.update( pdfrw.PdfDict(AP=data_dict[key], V=data_dict[key], F=0) )
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)

def Inport_csv_to_dict(input_pdf_path):
    with open(input_pdf_path,encoding="utf-8-sig") as fh:
        rd = csv.DictReader(fh, delimiter=',')
        for row in rd:
            data_dict = row
    return data_dict

if __name__ == '__main__':
    os.chdir(os.getcwd().replace(os.sep, '/'))
    data_dict = Inport_csv_to_dict(INPUT_CSV_PATH)
    write_fillable_pdf(INVOICE_TEMPLATE_PATH, INVOICE_OUTPUT_PATH, data_dict)
3
  • Is there any error or exception ? Commented Jan 16, 2020 at 7:10
  • Maybe the PDF is created in a place other than the one you are looking. Changing the CWD to the same directory is a no-op. It's not clear what that is intended to accomplish. Commented Jan 16, 2020 at 9:23
  • os.getcwd returns the current working directory, and os.chdir sets the current working directory, so os.chdir(os.getcwd().replace(os.sep, '/')) changes absolutely nothing. Changing backslash to slash here is pointless. The underlying WINAPI SetCurrentDirectoryW call first normalizes the path, which among other things, replaces slashes with the native backslash path separator. Commented Jan 17, 2020 at 6:17

1 Answer 1

1

Ok, this was kind of stupid from my part, but it works now.

I had the #! /usr/bin/python on the first line of my script. So the console run the script with python 2.

I removed the first line, and it worked as it should (with Python 3)

Sign up to request clarification or add additional context in comments.

1 Comment

If you have .py scripts associated with the py.exe launcher, then change the shebang to #!/usr/bin/python3 to ensure that it executes with Python 3.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.