V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Dec 25, 2020 · 2092 views
    This topic created in 1948 days ago, the information mentioned may be changed or developed.

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

    当我把计算题生成完了之后,再生成 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 replies    2020-12-25 15:56:00 +08:00
    no1xsyzy
        1
    no1xsyzy  
       Dec 25, 2020
    发 V2 ✔
    发 issue ✘
    读 manual ✘
    x1angw2
        2
    x1angw2  
    OP
       Dec 25, 2020
    @no1xsyzy j 就是看了 manual 没有解决才来发的。
    nano91
        3
    nano91  
       Dec 25, 2020
    excel 吧 转 pdf 就不怕打印有问题了
    no1xsyzy
        4
    no1xsyzy  
       Dec 25, 2020
    @x1angw2 我花了五分钟看了 Manual,TableStyle 里有个 'VALIGN'
    USAA
        6
    USAA  
       Dec 25, 2020
    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
       Dec 25, 2020
    @no1xsyzy 感谢大佬,我比较笨,也刚学不久。^-^
    x1angw2
        8
    x1angw2  
    OP
       Dec 25, 2020
    @zjsxwc 感谢,感谢。问题解决。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   877 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 22:03 · PVG 06:03 · LAX 15:03 · JFK 18:03
    ♥ Do have faith in what you're doing.