有了解 reportlab 这个库的吗?表格里面的文字怎么跑出来了?

2020-12-25 12:27:58 +08:00
 x1angw2

临近期末,我需要打印一些计算题,给班上的学生练习一下.

当我把计算题生成完了之后,再生成 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)

1569 次点击
所在节点    Python
8 条回复
no1xsyzy
2020-12-25 13:58:41 +08:00
发 V2 ✔
发 issue ✘
读 manual ✘
x1angw2
2020-12-25 14:01:11 +08:00
@no1xsyzy j 就是看了 manual 没有解决才来发的。
nano91
2020-12-25 14:04:48 +08:00
excel 吧 转 pdf 就不怕打印有问题了
no1xsyzy
2020-12-25 14:06:26 +08:00
@x1angw2 我花了五分钟看了 Manual,TableStyle 里有个 'VALIGN'
zjsxwc
2020-12-25 14:35:53 +08:00
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')
]
x1angw2
2020-12-25 15:55:33 +08:00
@no1xsyzy 感谢大佬,我比较笨,也刚学不久。^-^
x1angw2
2020-12-25 15:56:00 +08:00
@zjsxwc 感谢,感谢。问题解决。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/738856

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX