python - Python:PDF:如何读取复选框等表单元素
问题描述
我在 python 中使用 Reportlab API 创建了一个表单,例如带有一些复选框。
# simple_checkboxes.py
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfform
from reportlab.lib.colors import magenta, pink, blue, green
def create_simple_checkboxes():
c = canvas.Canvas('simple_checkboxes2.pdf')
#c.setFont("Courier", 20)
c.drawCentredString(300, 700, 'Pets')
#c.setFont("Courier", 14)
form = c.acroForm
c.drawString(10, 650, 'Dog:')
form.checkbox(name='cb1', tooltip='Field cb1',
x=110, y=645)
c.drawString(10, 600, 'Cat:')
form.checkbox(name='cb2', tooltip='Field cb2',
x=110, y=595)
c.drawString(10, 550, 'Pony:')
form.checkbox(name='cb3', tooltip='Field cb3',
x=110, y=545)
c.drawString(10, 500, 'Python:')
form.checkbox(name='cb4', tooltip='Field cb4',
x=110, y=495)
c.drawString(10, 450, 'Hamster:')
form.checkbox(name='cb5', tooltip='Field cb5',
x=110, y=445, checked=True)
c.save()
if __name__ == '__main__':
create_simple_checkboxes()
现在,我以后如何使用 Python 读取 PDF 中的输入,由用户填写?我没有找到关于 Reportlab API 的示例或文档。
还是有更好或更简单的方法来实现这一目标?
解决方案
我没有报告实验室的解决方案...
但是您可以使用 PyPDF2 :)
from PyPDF2 import PdfFileReader
import json
def getcheckboxes(filename):
with open(filename, "rb") as pdf:
pdfr = PdfFileReader(pdf)
pdf_fields = pdfr.getFields() #get field content of PDF File
rp = str(pdf_fields).replace("'", '"') #replace single quote with doudble to load string to dict
boxes = json.loads(rp) # string -> dict
for box in boxes: #loop over all checkboxes
print("checkbox {0} value is {1}".format(boxes[box]["/T"], boxes[box]["/V"])) #print out name = /T attribute and checked attribite = /V
getcheckboxes("test.pdf")
推荐阅读
- ios - 如何使用swift在ios应用程序中添加带有标题的超链接?
- azure - Azure B2C 错误:在技术配置文件中定义的 ID 为 \""objectId\"" 的查找声明无法找到声明
- spring-data-rest - 如何在 Spring Data REST 中自动扩展具有给定类型的所有关系?
- python - 如何在python中正确索引一个大矩阵
- r - r中条件str_replace_all的大小写敏感性
- javascript - 离子传递承诺到 RootParams
- javascript - 在momentjs中检查时间是否在两个小时之间的最佳方法
- css - 如何在 React 应用程序中使用 ::before CSS 伪选择器修改 Font Awesome Icon?
- java - JTable 中可编辑 JComboBox 的占位符
- prolog - Prolog - 模式匹配:由运算符链接的复杂表达式