V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
x1angw2
V2EX  ›  Python

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

  •  
  •   x1angw2 · 2020-12-25 12:27:58 +08:00 · 1556 次点击
    这是一个创建于 1239 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    当我把计算题生成完了之后,再生成 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)
    
    
    8 条回复    2020-12-25 15:56:00 +08:00
    no1xsyzy
        1
    no1xsyzy  
       2020-12-25 13:58:41 +08:00
    发 V2 ✔
    发 issue ✘
    读 manual ✘
    x1angw2
        2
    x1angw2  
    OP
       2020-12-25 14:01:11 +08:00
    @no1xsyzy j 就是看了 manual 没有解决才来发的。
    nano91
        3
    nano91  
       2020-12-25 14:04:48 +08:00
    excel 吧 转 pdf 就不怕打印有问题了
    no1xsyzy
        4
    no1xsyzy  
       2020-12-25 14:06:26 +08:00
    @x1angw2 我花了五分钟看了 Manual,TableStyle 里有个 'VALIGN'
    zjsxwc
        5
    zjsxwc  
       2020-12-25 14:35:53 +08:00
    USAA
        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')
    ]
    x1angw2
        7
    x1angw2  
    OP
       2020-12-25 15:55:33 +08:00
    @no1xsyzy 感谢大佬,我比较笨,也刚学不久。^-^
    x1angw2
        8
    x1angw2  
    OP
       2020-12-25 15:56:00 +08:00
    @zjsxwc 感谢,感谢。问题解决。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   938 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 20:26 · PVG 04:26 · LAX 13:26 · JFK 16:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.