1
下面的代碼創建一個包含99行數據的好測試表,並在每個分頁符處重複一個標題。這個表格非常窄,所以我想弄清楚如何進行拆分,使其具有第一頁左手邊的第1-37行,第一頁右手邊的第38-74行以及第二頁左邊的第75-99行。我稱之爲「跨頁分割表」,但可能會有一個更好的名稱,因此我希望我能夠準確描述它。如何在PDF頁面(並排)上分割ReportLab表格?
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Frame, Spacer
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A3, A4, landscape, portrait
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.pdfgen import canvas
pdfReportPages = "C:\\Temp\\test.pdf"
doc = SimpleDocTemplate(pdfReportPages, pagesize=A4)
# container for the "Flowable" objects
elements = []
styles=getSampleStyleSheet()
styleN = styles["Normal"]
# Make heading for each column and start data list
column1Heading = "COL ONE"
column2Heading = "COL TWO"
# Assemble data for each column using simple loop to append it into data list
data = [[column1Heading,column2Heading]]
for i in range(1,100):
data.append(["Col 1 Row " + str(i),"Col 2 Row " + str(i)])
tableThatSplitsOverPages = Table(data, [2.5 * cm, 2.5 * cm], repeatRows=1)
tableThatSplitsOverPages.hAlign = 'LEFT'
tblStyle = TableStyle([('TEXTCOLOR',(0,0),(-1,-1),colors.black),
('VALIGN',(0,0),(-1,-1),'TOP'),
('LINEBELOW',(0,0),(-1,-1),1,colors.black),
('BOX',(0,0),(-1,-1),1,colors.black),
('BOX',(0,0),(0,-1),1,colors.black)])
tblStyle.add('BACKGROUND',(0,0),(1,0),colors.lightblue)
tblStyle.add('BACKGROUND',(0,1),(-1,-1),colors.white)
tableThatSplitsOverPages.setStyle(tblStyle)
elements.append(tableThatSplitsOverPages)
doc.build(elements)
感謝Gordon--我希望能夠繼續使用SimpleDocTemplate,所以我會重新評估這個功能對我的應用程序的重要性,以及它是否保證我試圖學習如何修改頁面模板。是否可能有一個現有的頁面模板庫在某處選擇/學習? – PolyGeo 2012-02-22 22:49:11
不幸的是,我不認爲除了你可以在ReportLab網站上找到的東西以外。 – 2012-02-23 15:51:28