我将使用Python 2.7(iOS Pythonista应用程序)和reportlab 2.7模块创建一个包含表格的PDF,一切都运行良好。ReportLab会自动格式化列的宽度,但在两种情况下我无法理解为什么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. |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
案例二:我试图将浮点数对齐
在以下工具的帮助下:
TableStyle([('ALIGN', (3,1), (3,-1), 'DECIMAL'),])
我试图让每个浮点数都在同一竖直线上。这样做可以实现,但在“持续时间”列中,所有内容都对齐到右侧,导致“注释”列中的数字部分被遮盖(而且“持续时间”值的左侧有很多空白)。看起来好像是将右边距设置为小数点或小数点前的数字,而不是整个值。
+-----+----------+---------------+-----------+-------------------------------------------------+
| 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),])
我得到了更好的结果,但我认为应该有更好的方法!?
案例三:尝试换行笔记文本。
当我使用段落插入笔记文本时,文本会自动换行,但列宽令人惊讶。我无法像下面的示例一样精确地获得真实输出。
整个表格宽度适合文档(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)
我希望有人能指导我朝着正确的方向前进。