问题描述
我按照使用 Python 在 ReportLab 中创建交互式 PDF 表单
这是代码示例 esp.对于收音机:
simple_radios.py
from reportlab.pdfgen 导入画布从 reportlab.pdfbase 导入 pdfform从 reportlab.lib.colors 导入洋红色、粉色、蓝色、绿色def create_simple_radios():c = canvas.Canvas('simple_radios.pdf')c.setFont("Courier", 20)c.drawCentredString(300, 700, '电台演示')c.setFont("Courier", 14)表单 = c.acroFormc.drawString(10, 650, '狗:')form.radio(name='radio1', tooltip='Field radio1',值='值1',选择=假,x=110, y=645, buttonStyle='check',borderStyle='solid', shape='square',边框颜色=洋红色,填充颜色=粉红色,textColor=blue,forceBorder=True)form.radio(name='radio1', tooltip='Field radio1',值='值2',选择=真,x=110, y=645, buttonStyle='check',borderStyle='solid', shape='square',边框颜色=洋红色,填充颜色=粉红色,textColor=blue,forceBorder=True)c.drawString(10, 600, '猫:')form.radio(name='radio2', tooltip='Field radio2',值='值1',选择=真,x=110, y=595, buttonStyle='cross',borderStyle='solid', shape='circle',边框颜色=绿色,填充颜色=蓝色,边框宽度=2,textColor=粉红色,forceBorder=True)form.radio(name='radio2', tooltip='Field radio2',值='值2',选择=假,x=110, y=595, buttonStyle='cross',borderStyle='solid', shape='circle',边框颜色=绿色,填充颜色=蓝色,边框宽度=2,textColor=粉红色,forceBorder=True)c.drawString(10, 550, '小马:')form.radio(name='radio3', tooltip='Field radio3',值='值1',选择=假,x=110, y=545, buttonStyle='star',borderStyle='bevelled', shape='square',边框颜色=蓝色,填充颜色=绿色,边框宽度=2,textColor=洋红色,forceBorder=False)form.radio(name='radio3', tooltip='Field radio3',值='值2',选择=真,x=110, y=545, buttonStyle='star',borderStyle='bevelled', shape='circle',边框颜色=蓝色,填充颜色=绿色,边框宽度=2,textColor=洋红色,forceBorder=True)c.save()如果 __name__ == '__main__':create_simple_radios()
我对该代码的问题是:1.) 收音机始终处于推"状态.我怎样才能解除他们的压力?2.) 可以分组,以便根据组只按下一 (1) 个单选按钮3.) 我怎么能稍后以编程方式读取按钮的状态,例如通过 PyPDF2?
版本:
Python:3.7.3
报告实验室:3.5.19
枕头:6.0.0
PyPDF2:1.26.0
操作系统:
Windows10 v1809
按钮在 form.radio(... selected=True)
2.) 是否可以分组,以便根据组只按下一 (1) 个单选按钮?
name
属性与组名相关.
所以 form.radio(... name="group1")
是一组 form.radio(... name="group2")
第二组.每组只能选择一个收音机.
因此,对于前两个问题,我创建了一个包含两个不同组的简单示例.
第一个group
包含Fruits
,第二个包含Cars
:
from reportlab.pdfgen 导入画布从 reportlab.pdfbase 导入 pdfform从 reportlab.lib.colors 导入洋红色、粉色、蓝色、绿色、橙色、黄色def create_radios():c = canvas.Canvas('radios.pdf')c.setFont("Courier", 20)c.drawCentredString(300, 800, '电台演示')表单 = c.acroForm#组一,名称='组1'c.setFont("Courier", 16)c.drawString(10, 680, '水果:')c.setFont("Courier", 12)c.drawString(10, 650, '苹果:')form.radio(name='group1', tooltip='Apple',值='苹果',选择=假,x=110, y=650, buttonStyle='check',borderStyle='solid', shape='square',边框颜色=蓝色,填充颜色=洋红色,textColor=blue,forceBorder=True)c.drawString(10, 600, '香蕉:')form.radio(name='group1', tooltip='Banana',值='香蕉',选择=假,x=110, y=600, buttonStyle='check',borderStyle='solid', shape='square',边框颜色=蓝色,填充颜色=黄色,textColor=blue,forceBorder=True)c.drawString(10, 550, '橙色:')form.radio(name='group1', tooltip='Orange',值='橙色',选择=假,x=110, y=550, buttonStyle='check',borderStyle='solid', shape='square',边框颜色=蓝色,填充颜色=橙色,textColor=blue,forceBorder=True)#组二,名称='组2'c.setFont("Courier", 16)c.drawString(210, 680, '汽车:')c.setFont("Courier", 12)c.drawString(210, 650, '特斯拉:')form.radio(name='group2', tooltip='Apple',值='特斯拉',选择=假,x=310, y=650, buttonStyle='circle',borderStyle='solid', shape='circle',边框颜色=蓝色,填充颜色=洋红色,textColor=blue,forceBorder=False)c.drawString(210, 600, '奔驰:')form.radio(name='group2', tooltip='Banana',值='梅赛德斯',选择=假,x=310, y=600, buttonStyle='circle',borderStyle='solid', shape='circle',边框颜色=蓝色,填充颜色=洋红色,textColor=blue,forceBorder=False)c.drawString(210, 550, '丰田:')form.radio(name='group2', tooltip='Orange',价值=丰田",选择=假,x=310, y=550, buttonStyle='circle',borderStyle='solid', shape='circle',边框颜色=蓝色,填充颜色=洋红色,textColor=blue,forceBorder=False)c.save()如果 __name__ == '__main__':create_radios()
3.) 我怎样才能稍后以编程方式读取按钮的状态,例如通过 PyPDF2?
我找到了一种更简单的方法,然后使用 PyPDF2 返回的字段数据...
使用 pdfminer
可以很好地解决问题.
在创建 radios.pdf
后,我使用 Adobe
更改了值并将其保存为新文件 radios_checked.pdf
您也可以更改每组一个 selected
属性.
导入系统从 pdfminer.pdfparser 导入 PDFParser从 pdfminer.pdfdocument 导入 PDFDocument从 pdfminer.pdftypes 导入 resolve1文件名 = "radios_checked.pdf"使用 open(filename, 'rb') 作为 pdf_file:解析器 = PDFParser(pdf_file)doc = PDFDocument(解析器)fields = resolve1(doc.catalog['AcroForm'])['Fields']对于我在领域:字段 = resolve1(i)name = str(field.get('T'), 'utf-8')value = field.get('V') # 将返回 PSLiteral :/# 将 PSLiteral 转换为字符串如果值 != 无:值 = str(值)如果值[0] == r"/":值 = 值[2:-1]值 = str(值)打印(组名:{0},检查值:{1}".格式(名称,值))
这将过滤所有组对象并打印出选定的组名和选定的值.
提示:在文本编辑器中打开 pdf
并检查一般结构.
I have created a form with some radio buttons, following the examples from Creating Interactive PDF Forms in ReportLab with Python
Here is code example esp. for radios:
simple_radios.py
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfform
from reportlab.lib.colors import magenta, pink, blue, green
def create_simple_radios():
c = canvas.Canvas('simple_radios.pdf')
c.setFont("Courier", 20)
c.drawCentredString(300, 700, 'Radio demo')
c.setFont("Courier", 14)
form = c.acroForm
c.drawString(10, 650, 'Dog:')
form.radio(name='radio1', tooltip='Field radio1',
value='value1', selected=False,
x=110, y=645, buttonStyle='check',
borderStyle='solid', shape='square',
borderColor=magenta, fillColor=pink,
textColor=blue, forceBorder=True)
form.radio(name='radio1', tooltip='Field radio1',
value='value2', selected=True,
x=110, y=645, buttonStyle='check',
borderStyle='solid', shape='square',
borderColor=magenta, fillColor=pink,
textColor=blue, forceBorder=True)
c.drawString(10, 600, 'Cat:')
form.radio(name='radio2', tooltip='Field radio2',
value='value1', selected=True,
x=110, y=595, buttonStyle='cross',
borderStyle='solid', shape='circle',
borderColor=green, fillColor=blue,
borderWidth=2,
textColor=pink, forceBorder=True)
form.radio(name='radio2', tooltip='Field radio2',
value='value2', selected=False,
x=110, y=595, buttonStyle='cross',
borderStyle='solid', shape='circle',
borderColor=green, fillColor=blue,
borderWidth=2,
textColor=pink, forceBorder=True)
c.drawString(10, 550, 'Pony:')
form.radio(name='radio3', tooltip='Field radio3',
value='value1', selected=False,
x=110, y=545, buttonStyle='star',
borderStyle='bevelled', shape='square',
borderColor=blue, fillColor=green,
borderWidth=2,
textColor=magenta, forceBorder=False)
form.radio(name='radio3', tooltip='Field radio3',
value='value2', selected=True,
x=110, y=545, buttonStyle='star',
borderStyle='bevelled', shape='circle',
borderColor=blue, fillColor=green,
borderWidth=2,
textColor=magenta, forceBorder=True)
c.save()
if __name__ == '__main__':
create_simple_radios()
My problem/question with that code is:1.) The radios are always in a "pushed" state. How can I unpush them?2.) Can the be grouped, so that only ONE (1) radio button is pushed according to a group3.) How could I read the state of the buttons later on programmatically e.g. via PyPDF2?
Versions:
Python: 3.7.3
Reportlab: 3.5.19
Pillow: 6.0.0
PyPDF2: 1.26.0
OS:
Windows10 v1809
The buttons are pushed if form.radio(... selected=True)
The name
attribute is related to group name.
So form.radio(... name="group1")
is one group form.radio(... name="group2")
the second group. You can only select one radio each group.
So for the first two questions i've create a simple example with two different groups.
The first group
contains Fruits
and the second group contains Cars
:
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfform
from reportlab.lib.colors import magenta, pink, blue, green, orange, yellow
def create_radios():
c = canvas.Canvas('radios.pdf')
c.setFont("Courier", 20)
c.drawCentredString(300, 800, 'Radio demo')
form = c.acroForm
#GROUP ONE, name='group1'
c.setFont("Courier", 16)
c.drawString(10, 680, 'Fruits:')
c.setFont("Courier", 12)
c.drawString(10, 650, 'Apple:')
form.radio(name='group1', tooltip='Apple',
value='apple', selected=False,
x=110, y=650, buttonStyle='check',
borderStyle='solid', shape='square',
borderColor=blue, fillColor=magenta,
textColor=blue, forceBorder=True)
c.drawString(10, 600, 'Banana:')
form.radio(name='group1', tooltip='Banana',
value='banana', selected=False,
x=110, y=600, buttonStyle='check',
borderStyle='solid', shape='square',
borderColor=blue, fillColor=yellow,
textColor=blue, forceBorder=True)
c.drawString(10, 550, 'Orange:')
form.radio(name='group1', tooltip='Orange',
value='orange', selected=False,
x=110, y=550, buttonStyle='check',
borderStyle='solid', shape='square',
borderColor=blue, fillColor=orange,
textColor=blue, forceBorder=True)
#GROUP TWO, name='group2'
c.setFont("Courier", 16)
c.drawString(210, 680, 'Cars:')
c.setFont("Courier", 12)
c.drawString(210, 650, 'Tesla:')
form.radio(name='group2', tooltip='Apple',
value='tesla', selected=False,
x=310, y=650, buttonStyle='circle',
borderStyle='solid', shape='circle',
borderColor=blue, fillColor=magenta,
textColor=blue, forceBorder=False)
c.drawString(210, 600, 'Mercedes-Benz:')
form.radio(name='group2', tooltip='Banana',
value='mercedes', selected=False,
x=310, y=600, buttonStyle='circle',
borderStyle='solid', shape='circle',
borderColor=blue, fillColor=magenta,
textColor=blue, forceBorder=False)
c.drawString(210, 550, 'Toyota:')
form.radio(name='group2', tooltip='Orange',
value='toyota', selected=False,
x=310, y=550, buttonStyle='circle',
borderStyle='solid', shape='circle',
borderColor=blue, fillColor=magenta,
textColor=blue, forceBorder=False)
c.save()
if __name__ == '__main__':
create_radios()
I have found a simpler approach then using PyPDF2 returned field data...
Using pdfminer
will handle the problem well.
After i created the radios.pdf
i changed the values using Adobe
and saved it as new file radios_checked.pdf
also you could change one selected
attribute each group.
import sys
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdftypes import resolve1
filename = "radios_checked.pdf"
with open(filename, 'rb') as pdf_file:
parser = PDFParser(pdf_file)
doc = PDFDocument(parser)
fields = resolve1(doc.catalog['AcroForm'])['Fields']
for i in fields:
field = resolve1(i)
name = str(field.get('T'), 'utf-8')
value = field.get('V') #will return PSLiteral :/
# transform PSLiteral to string
if value != None:
value = str(value)
if value[0] == r"/":
value = value[2:-1]
value = str(value)
print("Group Name: {0}, checked value: {1} ".format(name , value))
This will filter all group objects and print out selected group name and selected value.
Hint:Open a pdf
in texteditor and check the general structure.
这篇关于Python:PDF:如何从带有单选按钮的表单中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!