因此,我尝试使用 python 中的 Sqlite 数据库填写 pdf 表单,但没有发生任何事情,输出文件为空。 这是我的代码(如果我确实写了不正确的东西或遗漏了一些东西,请不要犹豫谈论它):
import pdfrw
import sqlite3
pdf_template = "Fiche.pdf"
pdf_output = "FinaleFiche.pdf"
template_pdf = pdfrw.PdfReader(pdf_template)
ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_VAL_KEY = '/v'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'
for page in template_pdf.pages :
annotations = page[ANNOT_KEY]
for annotation in annotations:
if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
if annotation[ANNOT_FIELD_KEY]:
key = annotation[ANNOT_FIELD_KEY][1:-1]
conn = sqlite3.connect('user_data.db')
cur = conn.cursor()
cur.execute('SELECT * FROM My_choice')
data = [item[0:6]for item in cur.fetchall()]
print(data)
conn.close()
def fill_pdf (input_pdf_path , output_pdf_path, data):
template_pdf = pdfrw.PdfReader(input_pdf_path)
for page in template_pdf.pages:
annotations = page[ANNOT_KEY]
for annotation in annotations:
if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
if annotation[ANNOT_FIELD_KEY]:
key = annotation[ANNOT_FIELD_KEY][1:-1]
template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject("true")))
pdfrw.PdfWriter().write(output_pdf_path,template_pdf)
fill_pdf(pdf_template,pdf_output,data)