reportlab表格带有大文本字体

发布于 2025-02-09 12:59:12 字数 2151 浏览 1 评论 0原文

我是ReportLab的新手,但是很快就开始与Matplotlib结合使用它来生成一些不错的报告。现在,我面临的问题是在表中提供数据而不是图表,因此我决定查看ReportLab表来执行此操作,因为它似乎是工作的正确工具。 起初,一切都顺利进行,但是当我开始定制桌子样式以适应我的需求时,似乎表现得很奇怪。 当我的字体大小很小时,它会产生预期的结果,并且文本出现在单元格的中间,但是当我增加字体时,它开始漂移到单元的底部。这种行为的原因是什么?我已经查看了有关reportlab上表的文档,但在坐在那里时没有发现太多,所以那里没有运气。

这是我目前正在运行的代码,尽管中间有元素,但仍在单元格中产生一个非中间文本:

    tableData = [
                ['', 'Seg-Sex\n\n9h-18h', 'Seg-Sex\n\n18h-9h', 'Seg-Sex', 'Sab-Dom'],
                ['Secção 1', 'XXX kWh', 'XXX kWh', 'XXX kWh', 'XXX kWh'],  
                ['Secção 2', 'XXX kWh', 'XXX kWh', 'XXX kWh', 'XXX kWh'],  
                ['Secção 3', 'XXX kWh', 'XXX kWh', 'XXX kWh', 'XXX kWh'],  
                ['Total', 'XXX kWh', 'XXX kWh', 'XXX kWh', 'XXX kWh']  
            ]

    t = Table(tableData, colWidths=100, rowHeights=30, vAlign='middle')

    tableStyle = TableStyle([
        ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
        ('ALIGN', (0,0), (-1,-1), 'CENTER'),
        
        ('BACKGROUND',(1,1),(-2,-2),colors.HexColor(0x0039570F, hasAlpha=True)),
        ('BACKGROUND',(1,-1),(-1,-1),colors.HexColor(0x000000AB, hasAlpha=True)),
        ('TEXTCOLOR',(1,1),(-1,-2),colors.HexColor(0x003957)),
        ('INNERGRID',(0,0),(-1,-1),10,colors.red),
        #('BOX',(0,0),(-1,-1),1,colors.white),
        ('FONTNAME', (0,0), (-1,-1), 'Calibri'),
        ('FONTSIZE', (0,0), (-1,-1), 18),
        #('LEADING', (0,0), (-1,-1), 100),
        ('LEFTPADDING', (0,0), (-1,-1), 0),
        ('RIGHTPADDING', (0,0), (-1,-1), 0),
        ('BOTTOMPADDING', (0,0), (-1,-1), 0),
        ('TOPPADDING', (0,0), (-1,-1), 0),
    ])

    tableWidth, tableHeight = t.wrapOn(my_canvas, width, height)
    tableY = subSubTitleY-50-tableHeight
    tableX = width/2-tableWidth/2

    t.setStyle(tableStyle)
    t.drawOn(my_canvas, tableX, tableY)

使用此示例时,表中的所有单元都将其文本偏移到底部,即使将Valign设置为“中间”。当文本大小较小时,例如:{'fontsize',(0,0),(-1,-1),10)而不是18

。结果:

我的一个可能的解决方案是用“拖网”和其他帆布支持的方法组装表,但想先尝试使用此解决方案。 事先感谢您对此问题的任何帮助。

I'm fairly new to reportlab but pretty quickly started to generate some nice reports with it in conjunction with matplotlib. Now I'm faced with the problem of presenting data in a table rather then a chart so I decided to look into reportlab Tables to do it as it seemed the right tool for the job.
At first everything was going smoothly but when I started to customized the table style to fit my needs VALIGN seems to behave strangely.
When I have a small fontsize it produces the expected result and the text appears in the middle of the cell but when I increase the fontsize it start to drift to the bottom of the cell. What is the reason for this kind of behaviour? I have looked at the documentation in regards to Tables on reportlab but haven't found much when it comes to VALIGN so no luck there.

Here is the code that I'm currently running and is producing a non middle text in cells despite having middle VALIGN:

    tableData = [
                ['', 'Seg-Sex\n\n9h-18h', 'Seg-Sex\n\n18h-9h', 'Seg-Sex', 'Sab-Dom'],
                ['Secção 1', 'XXX kWh', 'XXX kWh', 'XXX kWh', 'XXX kWh'],  
                ['Secção 2', 'XXX kWh', 'XXX kWh', 'XXX kWh', 'XXX kWh'],  
                ['Secção 3', 'XXX kWh', 'XXX kWh', 'XXX kWh', 'XXX kWh'],  
                ['Total', 'XXX kWh', 'XXX kWh', 'XXX kWh', 'XXX kWh']  
            ]

    t = Table(tableData, colWidths=100, rowHeights=30, vAlign='middle')

    tableStyle = TableStyle([
        ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
        ('ALIGN', (0,0), (-1,-1), 'CENTER'),
        
        ('BACKGROUND',(1,1),(-2,-2),colors.HexColor(0x0039570F, hasAlpha=True)),
        ('BACKGROUND',(1,-1),(-1,-1),colors.HexColor(0x000000AB, hasAlpha=True)),
        ('TEXTCOLOR',(1,1),(-1,-2),colors.HexColor(0x003957)),
        ('INNERGRID',(0,0),(-1,-1),10,colors.red),
        #('BOX',(0,0),(-1,-1),1,colors.white),
        ('FONTNAME', (0,0), (-1,-1), 'Calibri'),
        ('FONTSIZE', (0,0), (-1,-1), 18),
        #('LEADING', (0,0), (-1,-1), 100),
        ('LEFTPADDING', (0,0), (-1,-1), 0),
        ('RIGHTPADDING', (0,0), (-1,-1), 0),
        ('BOTTOMPADDING', (0,0), (-1,-1), 0),
        ('TOPPADDING', (0,0), (-1,-1), 0),
    ])

    tableWidth, tableHeight = t.wrapOn(my_canvas, width, height)
    tableY = subSubTitleY-50-tableHeight
    tableX = width/2-tableWidth/2

    t.setStyle(tableStyle)
    t.drawOn(my_canvas, tableX, tableY)

All cells in the table have their text offset to the bottom when using this example even though VALIGN is set to 'middle'. This does no appear to be the issue when the text size is smaller for instance with: ('FONTSIZE', (0,0), (-1,-1), 10) instead of 18.

Here is a sample picture of the result:
enter image description here

One possible solution I though of was to assemble the table with "drawStrings" and other canvas supported methods but wanted to try this solution first.
Thanks in advance for any help regarding this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文