【问题标题】:Why does VALIGN not work in a ReportLab Table?为什么 VALIGN 在 ReportLab 表中不起作用?
【发布时间】:2021-05-30 01:11:44
【问题描述】:

我一直在尝试使用reportlab 生成表格,但在使用TableStyle 中的VALIGN 命令时遇到了问题。它似乎没有任何效果。每当我创建表格时,文本都会与网格线重叠,VALIGN 命令对文本的位置没有影响。下面是一个重现此效果的玩具示例。下图显示了重叠的文本。

from reportlab.pdfgen.canvas import Canvas
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import Paragraph, Frame, Table, Spacer, TableStyle
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import inch
from datetime import datetime
from random import random


from reportlab.pdfgen import canvas

c = Canvas('L:/table_test.pdf', pagesize=letter, bottomup=False)
width, height = letter

CENTER_X = width/2

#c.translate(inch, inch) # Set origin
c.setFont("Times-Roman", 12) # Perfecting font

# Draw a shape
c.setStrokeColorRGB(0, 0, 0)
c.setFillColorRGB(0, 0, 0)

rand_data = [[round(random(), 2) for x in range(10)] for k in range(10)]
t = Table(rand_data)
t.setStyle(TableStyle([("BOX", (0,0), (-1, -1), 0, colors.black),
                       ('INNERGRID', (0,0), (-1,-1), 0, colors.black),
                       ('VALIGN',(0,0),(-1,-1),'BOTTOM'),
                       ('ALIGN',(0,0),(-1,-1),'CENTER'),
                       ('FONT',(0,-1), (-1,-1), 'Times-Bold'),]))

t.wrapOn(c, 0, 0)
t.drawOn(c, 1*inch, 1.5*inch)

c.showPage()
c.save()

我的桌子是什么样子的

【问题讨论】:

    标签: python reportlab


    【解决方案1】:

    我认为bottomup 选项已损坏,除1(默认)以外的任何值,文本呈现不正确。

    TableVALIGN需要指定rowHeights才能生效,否则单元格高度调整为字体高度+内边距。

    t = Table(rand_data, rowHeights = inch)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 2012-01-23
      • 2019-02-01
      • 2012-11-26
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多