gpt4 book ai didi

Python 和报告实验室 : wrong column width and alignment in PDF table

转载 作者:太空狗 更新时间:2023-10-30 02:20:24 24 4
gpt4 key购买 nike

我使用 Python 2.7(iOS Pythonista 应用程序)和 reportlab 2.7 模块创建带有表格的 PDF。一切正常。 RepotLab 自动格式化列的宽度。但在两种情况下,我不明白为什么 reportlab 会按照它的方式格式化输出,以及我如何才能获得我想要的格式。

案例 1:几乎如我所愿……

+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Day | Date | Time | Duration | Notes |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Tue | 01.04.14 | 14:00 - 17:15 | 3.25 | Here are some notes. |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Wed | 02.04.14 | 18:00 - 20:15 | 2.25 | Sometime these notes are a little longer text so there must be a line break to let the whole note be visible! |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
| Thu | 02.04.14 | 14:00 - 17:15 | 3.25 | And sometimes these notes are only a few words. |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+

情况 2:我尝试排列 float

借助于

TableStyle([('ALIGN', (3,1), (3,-1), 'DECIMAL'),])

我试图让每个浮点都恰好在一条垂直线上。它有效,但在 Duration 列中,所有内容都如此(硬)对齐到右侧,以至于 Notes 列中的一部分数字(并且 Duration 值的左侧有很多空间)。似乎右填充设置为点或点之前的数字而不是整个值。

+-----+----------+---------------+-----------+-------------------------------------------------+
| Day | Date | Time | Duration | Notes |
+-----+----------+---------------+-----------+-------------------------------------------------+
| Tue | 01.04.14 | 14:00 - 17:15 | 3.2|5Here are some notes. |
+-----+----------+---------------+-----------+-------------------------------------------------+
| Wed | 02.04.14 | 14:00 - 17:15 | 3.2|5And sometimes these notes are only a few words. |
+-----+----------+---------------+-----------+-------------------------------------------------+

借助于

TableStyle([('RIGHTPADDING', (3,1), (3,-1), 18),])

我得到了更好的结果,但我认为应该有更好的方法!?

案例 3:尝试将注释文本换行。

当我使用段落插入注释文本时,我得到了环绕文本,但列宽惊人。我无法得到与实际输出完全相同的以下示例。

整个表格宽度适合文档(没有边距的 DIN A4)。但似乎每一列的宽度都相等。因此 Day、Date、Time 和 Duration 列比它们需要的要宽得多,而 Notes 列比它们需要的要窄得多。我无法在下面的示例表中展示这两件事,但我相信你的想象力;-)。

+-----+----------+---------------+----------+----------------------+
| Day | Date | Time | Duration | Notes |
+-----+----------+---------------+----------+----------------------+
| Tue | 01.04.14 | 14:00 - 17:15 | 3.25 | Here are some notes. |
+-----+----------+---------------+----------+----------------------+
| Wed | 02.04.14 | 18:00 - 20:15 | 2.25 | Sometime these |
| | | | | notes are a |
| | | | | little longer |
| | | | | text so there |
| | | | | must be a line |
| | | | | break to let |
| | | | | the whole note |
| | | | | be visible! |
+-----+----------+---------------+----------+----------------------+
| Thu | 02.04.14 | 14:00 - 17:15 | 3.25 | And sometimes |
| | | | | these notes are |
| | | | | only a few words. |
+-----+----------+---------------+----------+----------------------+

这是我使用的代码:

import pipista
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, TableStyle

styleSheet = getSampleStyleSheet()

def makeReportData(n):
monthreport = []
monthreport += ('Day', '', 'Time', 'Duration', 'Notes'),
monthreport += ('Tue', '01.04.14', '14:00 - 17:15', '3.25', n[0]),
monthreport += ('Wed', '02.04.14', '14:00 - 17:15', '3.25', n[1]),
monthreport += ('Thu', '03.04.14', '14:00 - 17:15', '3.25', n[2]),
return monthreport

notes = ['Here are some notes.',
'Sometime these notes are a little longer text so there must be a line break to let the whole note be visible!',
'And sometimes these notes are only a few words.']

paranote = []
for note in notes:
paranote += (Paragraph(note, styleSheet["BodyText"]),)

tstyle = [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),]

case1 = []
t = Table(makeReportData(notes))
t.setStyle(TableStyle(tstyle))
case1.append(t)
doc = SimpleDocTemplate('Table normal.pdf', pagesize = A4)
doc.build(case1)

case2 = []
tstyledez = tstyle + [('ALIGN', (3,1), (3,-1), 'DECIMAL'),]
t.setStyle(TableStyle(tstyledez))
case2.append(t)
doc = SimpleDocTemplate('Table align decimal.pdf', pagesize = A4)
doc.build(case2)

case3 = []
t = Table(makeReportData(paranote))
tstylepara = tstyle + [('VALIGN',(0,1),(3,-1),'TOP'),]
t.setStyle(TableStyle(tstylepara))
case3.append(t)
doc = SimpleDocTemplate('Table Paragraph.pdf', pagesize = A4)
doc.build(case3)

我希望有人能把我推向正确的方向。

最佳答案

好的,RTM 总是一个好主意!来自 ReportLab 文档:

If a cell value is a Flowable or list of Flowables these must either have a determined width or the containing column must have a fixed width.

解决方法:

from reportlab.lib.units import mm

t = Table(makeReportData(paranote), colWidths=(None, None, None, None, 100*mm))

我想也许 DECIMAL 对齐的问题(案例 2)可以用同样的方式解决,但那行不通。

关于Python 和报告实验室 : wrong column width and alignment in PDF table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23546985/

24 4 0