我使用Python 2.7(iOS Pythonista App)和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. |
+-----+----------+---------------+----------+---------------------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
案例2:我尝试排列浮点数
在...的帮助下
TableStyle([('ALIGN', (3,1), (3,-1), 'DECIMAL'),])
Run Code Online (Sandbox Code Playgroud)
我试图将每个浮点精确地放在一条垂直线上.它可以工作,但是在"持续时间"列中,所有内容都是如此(硬)对齐,右侧是"注释"列中数字的一部分(并且"持续时间"值左侧有很多空格).似乎正确的填充设置为点之前的点或数字,而不是整个值.
+-----+----------+---------------+-----------+-------------------------------------------------+
| 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. |
+-----+----------+---------------+-----------+-------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)
在...的帮助下
TableStyle([('RIGHTPADDING', (3,1), (3,-1), 18),])
Run Code Online (Sandbox Code Playgroud)
我得到了更好的结果,但我认为应该有更好的方法!?
案例3:尝试包装备注文本.
当我使用段落插入注释文本时,我得到包装文本,但列宽是惊人的.我无法完全按照实际输出得到以下示例.
整个桌子宽度适合文件(没有边距的DIN A4).但似乎每列都有相同的宽度.因此,日期,日期,时间和持续时间列比它们需要的范围要宽得多,并且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. |
+-----+----------+---------------+----------+----------------------+
Run Code Online (Sandbox Code Playgroud)
这是我使用的代码:
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)
Run Code Online (Sandbox Code Playgroud)
我希望有人能把我推向正确的方向.
好的,RTM总是个好主意!从ReportLab文档:
如果单元格值是Flowable或Flowable列表,则这些值必须具有确定的宽度,或者包含列必须具有固定宽度.
解:
from reportlab.lib.units import mm
t = Table(makeReportData(paranote), colWidths=(None, None, None, None, 100*mm))
Run Code Online (Sandbox Code Playgroud)
我想也许DECIMAL对齐(案例2)的问题可能以同样的方式解决,但这不起作用.
| 归档时间: |
|
| 查看次数: |
5989 次 |
| 最近记录: |