临近期末,我需要打印一些计算题,给班上的学生练习一下.
当我把计算题生成完了之后,再生成 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)
1
no1xsyzy 2020-12-25 13:58:41 +08:00
发 V2 ✔
发 issue ✘ 读 manual ✘ |
3
nano91 2020-12-25 14:04:48 +08:00
excel 吧 转 pdf 就不怕打印有问题了
|
5
zjsxwc 2020-12-25 14:35:53 +08:00
|
6
USAA 2020-12-25 14:46:51 +08:00
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') ] |