【问题标题】:VALIGN in reportlab TableStyle apparently without effectreportlab TableStyle中的VALIGN显然没有效果
【发布时间】:2016-09-01 10:16:26
【问题描述】:

所以,一段时间以来,我一直在努力解决这个问题。我知道有很多类似的问题有很好的答案,这些答案我都试过了,但我得到的代码基本上反映了给出的答案。

我正在编写代码来自动为工作表生成匹配的练习。所有这些信息都应该在一个表格中。并且文本应该都与单元格的顶部对齐。

这是我现在拥有的:

from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table,         TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import cm

document = []
doc = SimpleDocTemplate('example.pdf', pagesize=A4, rightMargin=72, leftMargin=72, topMargin=72)
styles = getSampleStyleSheet()

definitions = []
i, a = 1, 65
table = []
for x in range(1, 10):
    line = []
    line.append(Paragraph(str(i), styles['BodyText']))
    line.append(Paragraph('Vocabulary', styles['BodyText']))
    line.append(Paragraph(chr(a), styles['BodyText']))
    line.append(Paragraph('Often a multi-line definition of the vocabulary. But then, sometimes something short and sweet.', styles['BodyText']))
    table.append(line)
    i += 1
    a += 1

t = Table(table, colWidths=(1*cm, 4*cm, 1*cm, None))
t.setStyle(TableStyle([
    ('VALIGN', (1, 1), (-1, -1), 'TOP')
]))

document.append(t)
doc.build(document)

我忽略了什么?

【问题讨论】:

  • 喜欢你个人资料中的故事,我希望我高中的老师也能有同样的态度!
  • 谢谢!我不得不回去再读一遍,但它仍然是真的。我不会撒谎:今天它给了我几根白发。

标签: python formatting reportlab


【解决方案1】:

问题在于您索引TableStyle 的方式。 Reportlab 中的索引从第一行第一列的(0, 0) 开始。因此,在您的情况下,(1, 1) 仅将样式应用于第一行下方和第一列右侧的所有内容。

正确的方法是使用:

('VALIGN', (0, 0), (-1, -1), 'TOP')

这会将样式应用于Table 中的所有单元格。

【讨论】:

  • 感谢耐心解答。那效果很好。我不知道为什么我自己看不到,但我很高兴你看到了。
  • 有没有办法将文本与 top_center 对齐?.. 我试过使用 ('VALIGN', (0, 0), (-1, -1), 'TOP_CENTER') 但它基本上将文本对齐到单元格的中心而不是顶部中心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多