2012-02-14 77 views
10

我使用ReportLab在PDF文檔中編寫表格,對結果非常滿意(儘管尚未完全掌握流動模型)。如何在ReportLab的PDF輸出中重複表格列標題頁面中的分頁符

但是,我一直無法弄清楚如何製作跨越分頁符的表格,並重複列標題。

下面的代碼在C:\ Temp中創建一個test.pdf,它有一個標題行,後跟99行數據。

標題行在第一頁看起來不錯,但我希望在第二頁和第三頁的頂部重複。

我很想聽聽使用SimpleDocTemplate完成該操作的任何方法。

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 
column1Heading = Paragraph("<para align=center>COLUMN ONE HEADING</para>",styles['Normal']) 
column2Heading = Paragraph("<para align=center>COLUMN TWO HEADING</para>",styles['Normal']) 
row_array = [column1Heading,column2Heading] 
tableHeading = [row_array] 
tH = Table(tableHeading, [6 * cm, 6 * cm])   # These are the column widths for the headings on the table 
tH.hAlign = 'LEFT' 
tblStyle = TableStyle([('TEXTCOLOR',(0,0),(-1,-1),colors.black), 
         ('VALIGN',(0,0),(-1,-1),'TOP'), 
         ('BOX',(0,0),(-1,-1),1,colors.black), 
         ('BOX',(0,0),(0,-1),1,colors.black)]) 
tblStyle.add('BACKGROUND',(0,0),(-1,-1),colors.lightblue) 
tH.setStyle(tblStyle) 
elements.append(tH) 

# Assemble rows of data for each column 
for i in range(1,100): 
    column1Data = Paragraph("<para align=center> " + "Row " + str(i) + " Column 1 Data" + "</font> </para>",styles['Normal']) 
    column2Data = Paragraph("<para align=center> " + "Row " + str(i) + " Column 2 Data" + "</font> </para>",styles['Normal']) 
    row_array = [column1Data,column2Data] 
    tableRow = [row_array] 
    tR=Table(tableRow, [6 * cm, 6 * cm]) 
    tR.hAlign = 'LEFT' 
    tR.setStyle(TableStyle([('BACKGROUND',(0,0),(-1,-1),colors.white), 
          ('TEXTCOLOR',(0,0),(-1,-1),colors.black), 
          ('VALIGN',(0,0),(-1,-1),'TOP'), 
          ('BOX',(0,0),(-1,-1),1,colors.black), 
          ('BOX',(0,0),(0,-1),1,colors.black)])) 
    elements.append(tR) 
    del tR 

elements.append(Spacer(1, 0.3 * cm)) 

doc.build(elements) 

回答

11

從文檔(是的,我知道,但它有時很難找到手冊中的這個東西):

的repeatRows參數指定 應該被重複時,領先的行數該表被要求自行拆分。

因此,當您創建表時,這是您可以傳遞的參數之一,它會將前n行轉換爲重複的標題行。你會發現第77頁上的文字的這部分,但與創建一個表段第76頁。

http://www.reportlab.com/docs/reportlab-userguide.pdf

+0

感謝戈登 - 指點我回到了手動已經使我能夠開發一個更簡單和有效的代碼示例,我將在下面添加一個答案。 – PolyGeo 2012-02-15 04:16:25

15

上啓動這是我開發的代碼,以下戈登的建議使用repeatRows重新考慮後,它的工作原理!

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 = "COLUMN ONE HEADING" 
column2Heading = "COLUMN TWO HEADING" 
# 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([str(i),str(i)]) 

tableThatSplitsOverPages = Table(data, [6 * cm, 6 * 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) 
-3

我發現這個解決方案可以輕鬆地在兩頁的表格上重複標題。在您的表格的CSS中添加以下行:

-fs-table-paginate:paginate;

我還發現了一個類FPDF這似乎是強大的(我並不需要它的時刻,所以我沒有測試它)

http://interpid.eu/fpdf-table

+0

感謝您的建議,但在我的情況下,我不使用CSS - 除非發生在後臺。 – PolyGeo 2013-04-29 02:05:49

相關問題