python form enter data

Solutions on MaxInterview for python form enter data by the best coders in the world

showing results for - "python form enter data"
Sasha
03 Jul 2019
1import os
2import sys
3from collections import OrderedDict
4from PyPDF2 import PdfFileReader
5
6def _getFields(obj, tree=None, retval=None, fileobj=None):
7    fieldAttributes = {'/FT': 'Field Type', '/Parent': 'Parent', '/T': 'Field Name', 
8    '/TU': 'Alternate Field Name', '/TM': 'Mapping Name', '/Ff': 'Field Flags', 
9    '/V': 'Value', '/DV': 'Default Value'}
10    if retval is None:
11        retval = OrderedDict()
12        catalog = obj.trailer["/Root"]
13        if "/AcroForm" in catalog:
14            tree = catalog["/AcroForm"]
15        else:
16            return None
17    if tree is None:
18        return retval
19
20    obj._checkKids(tree, retval, fileobj)
21    for attr in fieldAttributes:
22        if attr in tree:
23            obj._buildField(tree, retval, fileobj, fieldAttributes)
24            break
25
26    if "/Fields" in tree:
27        fields = tree["/Fields"]
28        for f in fields:
29            field = f.getObject()
30            obj._buildField(field, retval, fileobj, fieldAttributes)
31
32    return retval
33
34def get_form_fields(infile):
35    infile = PdfFileReader(open(infile, 'rb'))
36    fields = _getFields(infile)
37    return OrderedDict((k, v.get('/V', '')) for k, v in fields.items())
38
39def selectListOption(all_lines, k, v):
40    all_lines.append('function setSelectedIndex(s, v) {')
41    all_lines.append('for (var i = 0; i < s.options.length; i++) {')
42    all_lines.append('if (s.options[i].text == v) {')
43    all_lines.append('s.options[i].selected = true;')
44    all_lines.append('return;') 
45    all_lines.append('}')
46    all_lines.append('}')
47    all_lines.append('}')
48    all_lines.append('setSelectedIndex(document.getElementById("' + k + '"), "' + v + '");')
49
50def readList(fname):
51    lst = []
52    with open(fname, 'r') as fh:  
53        for l in fh:
54            lst.append(l.rstrip(os.linesep))
55    return lst
56
57def createBrowserScript(fl, fl_ext, items, pdf_file_name):
58    if pdf_file_name and len(fl) > 0:
59        of = os.path.splitext(pdf_file_name)[0] + '.txt'
60        all_lines = []
61        for k, v in items.items():
62            print(k + ' -> ' + v)
63            if (v in ['/Yes', '/On']):
64                all_lines.append("document.getElementById('" + k + "').checked = true;\n");
65            elif (v in ['/0'] and k in fl_ext):
66                all_lines.append("document.getElementById('" + k + "').checked = true;\n");
67            elif (v in ['/No', '/Off', '']):
68                all_lines.append("document.getElementById('" + k + "').checked = false;\n");
69            elif (v in [''] and k in fl_ext):
70                all_lines.append("document.getElementById('" + k + "').checked = false;\n");
71            elif (k in fl):
72                selectListOption(all_lines, k, v)
73            else:
74                all_lines.append("document.getElementById('" + k + "').value = '" + v + "';\n");
75        outF = open(of, 'w')
76        outF.writelines(all_lines)
77        outF.close()
78
79def execute(args):
80    try: 
81        fl = readList('myview.ini')
82        fl_ext = readList('myview_ext.ini')
83        if len(args) == 2:
84            pdf_file_name = args[1]
85            items = get_form_fields(pdf_file_name)
86            createBrowserScript(fl, fl_ext, items, pdf_file_name)
87        else:
88            files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.pdf')]
89            for f in files:
90                items = get_form_fields(f)
91                createBrowserScript(fl, fl_ext, items, f)
92    except BaseException as msg:
93        print('An error occurred... :( ' + str(msg))
94
95if __name__ == '__main__':
96    from pprint import pprint
97    execute(sys.argv)
98