临近期末,我需要打印一些计算题,给班上的学生练习一下.
当我把计算题生成完了之后,再生成 pdf 文档打印。我没有用 excel,因为在其它电脑上使用可能会出现打印预览的问题。所以选择了 reportlab 生成 pdf 把格式固定下来,打印也方便了很多。
from reportlab.platypus import Table,TableStyle
使用这 table 生成表格,tableStyle 调整表格样式。
当我使用默认的格式是这样的:
# Title
elements = []
# Table
table_date = [
['98 + 12= ','1 + 3 =','3 * 9 =','81 / 9 ='],
]
table_style = [
('GRID',(0,0),(-1,-1),1,colors.black),
('FONTSIZE',(0,0),(-1,-1),13),
('ALIGN',(0,0),(-1,-1),'LEFT'),
# ('FONTNAME',(0,0),(-1,-1),'mysh')
]
mytable = Table(table_date)
mytable_style = TableStyle(table_style)
mytable.setStyle(mytable_style)
elements.append(mytable)
doc = SimpleDocTemplate(
"tmp.pdf",
pagesize = A4,
)
doc.build(elements)
表格和字体就太小了,我先把表格的高度和宽度作为调整:
#mytable = Table(table_date)
mytable = Table(table_date,colWidths=125,rowHeights=32)
指定了高度和宽度。
table_style = [
('GRID',(0,0),(-1,-1),1,colors.black),
('FONTSIZE',(0,0),(-1,-1),13),
('ALIGN',(0,0),(-1,-1),'LEFT'),
# ('FONTNAME',(0,0),(-1,-1),'mysh')
]
我把这个 ('FONTSIZE',(0,0),(-1,-1),13),
的 13 修改成 20 。
table_style = [
('GRID',(0,0),(-1,-1),1,colors.black),
('FONTSIZE',(0,0),(-1,-1),20),
('ALIGN',(0,0),(-1,-1),'LEFT'),
# ('FONTNAME',(0,0),(-1,-1),'mysh')
]
如图所示,算式直接跑出来了。 有知道的大佬帮下忙呗。 下面是所有代码和预览图,刚学,写的比较乱。
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import getSampleStyleSheet,ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer,Image,Table,TableStyle,Frame,ListFlowable, ListItem
from reportlab.lib.enums import TA_JUSTIFY,TA_CENTER,TA_LEFT
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4,B9,LETTER
from reportlab.lib.units import inch
import os,time,sys
pdfmetrics.registerFont(TTFont('mysh','my.ttf'))
pdfmetrics.registerFont(TTFont('py','Pinyin.ttf'))
class_name = '张三'
# Title
elements = []
world_title_style = ParagraphStyle(
name = 'title_Style',
fontName = 'py',
fontSize = 35,
leading = 0,
alignment = TA_CENTER,
spaceAfter = 6
)
world_class_name_style = ParagraphStyle(
name = 'class_and_Style',
fontName = 'py',
fontSize = 25,
leading = 30,
alignment = TA_LEFT,
spaceAfter = 6
)
world_title = '二年级数学上册计算练习题'
world_class_name = '姓名:' + class_name
elements.append(Paragraph(world_title,style=world_title_style))
elements.append(Spacer(1,0.7*inch))
elements.append(Paragraph(world_class_name,style=world_class_name_style))
elements.append(Spacer(1,0.2*inch))
# Table
table_date = [
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
['98+12=','1+3=','3*9=','81/9='],
]
table_style = [
('GRID',(0,0),(-1,-1),1,colors.black),
('FONTSIZE',(0,0),(-1,-1),20),
('ALIGN',(0,0),(-1,-1),'LEFT'),
# ('FONTNAME',(0,0),(-1,-1),'mysh')
]
mytable = Table(table_date,colWidths=125,rowHeights=32)
mytable_style = TableStyle(table_style)
mytable.setStyle(mytable_style)
elements.append(mytable)
doc = SimpleDocTemplate(
"just_test.pdf",
pagesize = A4,
topMargin = 20,
bottomMargin = 20
)
doc.build(elements)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.