我使用Python 2.7(iOS Pythonista應用程序)和reportlab 2.7模塊來創建一個帶有表格的PDF。一切正常。 RepotLab自動格式化列的寬度。但在兩種情況下,我無法理解爲什麼reportlab以它的方式格式化輸出,以及如何獲得我想要的格式。Python&ReportLab:在PDF表中錯誤的列寬和對齊方式
案例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:我嘗試排隊浮動點
隨着
TableStyle([('ALIGN', (3,1), (3,-1), 'DECIMAL'),])
幫助我試圖將每個浮點都精確地放在一條垂直線上。它可以工作,但在持續時間列中,所有事情都是如此(難)對齊到Notes列中數字的一部分(並且持續時間值左側有很多空間)。似乎正確的填充設置爲點或點之前的數字,而不是整個值。
+-----+----------+---------------+-----------+-------------------------------------------------+
| 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)。但是,似乎每列都有相同的寬度。因此,日期,日期,時間和持續時間列比他們需要的寬得多,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)
我希望有人可以把我在正確的方向。
很明顯,原始海報確實是從這個問題中的大量例子中看出來的。 – shaw2thefloor