理解回溯错误[DJANGO/PYTHON]
我试图理解一个回溯错误。见下文 我所做的是用Python操作PDF表单字段。 Im使用pdftk和fdfgen,仅使用pythonim操作PDF表单字段,如下示例 我的意见是:理解回溯错误[DJANGO/PYTHON],python,django,subprocess,pdftk,fdfgen,Python,Django,Subprocess,Pdftk,Fdfgen,我试图理解一个回溯错误。见下文 我所做的是用Python操作PDF表单字段。 Im使用pdftk和fdfgen,仅使用pythonim操作PDF表单字段,如下示例 我的意见是: def combine_pdfs(list_of_pdfs, outfile): ''' Use pdftk to combine multiple pdfs into a signle pdf ''' call = ['pdftk'] call += list_of_pdfs
def combine_pdfs(list_of_pdfs, outfile):
'''
Use pdftk to combine multiple pdfs into a signle pdf
'''
call = ['pdftk']
call += list_of_pdfs
call += ['cat', 'output']
call += [outfile]
print(" ".join(call))
try:
data_string = check_output(call).decode('utf8')
except IOError:
raise PdftkNotInstalledError('Could not locate PDFtk installation')
return outfile
def get_fields(pdf_file):
'''
Use pdftk to get a pdf's fields as a string, parse the string
and return the fields as a dictionary, with field names as keys
and field values as values.
'''
fields = {}
call = ['pdftk', pdf_file, 'dump_data_fields_utf8']
try:
data_string = check_output(call).decode('utf8')
except IOError:
raise PdftkNotInstalledError('Could not locate PDFtk installation')
data_list = data_string.split('\r\n')
if len(data_list) == 1:
data_list = data_string.split('\n')
for line in data_list:
if line:
re_object = match(r'(\w+): (.+)', line)
if re_object is not None:
if re_object.group(1) == 'FieldName':
key = re_object.group(2)
fields[key] = ''
elif re_object.group(1) == 'FieldValue':
fields[key] = re_object.group(2)
return fields
def write_pdf(source, fields, output, flatten=False):
'''
Take a source file path, list or dictionary of fdf fields, and
output path, and create a filled-out pdf.
'''
fdf = forge_fdf(fdf_data_strings=fields)
with NamedTemporaryFile(delete=False) as file:
file.write(fdf)
call = ['pdftk', source, 'fill_form', file.name, 'output', output]
if flatten:
call.append('flatten')
try:
check_output(call)
except IOError:
raise PdftkNotInstalledError('Could not locate PDFtk installation')
remove(file.name)
class PdftkNotInstalledError(Exception):
pass
def main(request,pk):
if pk:
obj = MyModel.objects.get(pk=pk)
if obj.text == 'Secure':
pdf_path = "/home/myproject/webapps/app/app/home/templates/home/fill.pdf"
fields = get_fields(pdf_path)
fields['F[0].P1[0].Name[0]'] = obj.name
write_pdf(pdf_path,fields,'output.pdf')
return HttpResponseRedirect('/Search')
url.py是:
url(r'^pdf_test/(?P<pk>[-\w]+)/$', views.main, name='pdf_test')
url(r'^pdf\u test/(?P[-\w]+)/$”,views.main,name='pdf\u test')
url(r'^pdf_test/(?P<pk>[-\w]+)/$', views.main, name='pdf_test')