2011-11-01 60 views
1

實施例:ReportLab的:使用表和SPAN

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

from reportlab.platypus import SimpleDocTemplate, Table, TableStyle 
from reportlab.lib.pagesizes import letter 

def testPdf(): 
    doc = SimpleDocTemplate("testpdf.pdf",pagesize=letter, 
         rightMargin=72,leftMargin=72, 
         topMargin=72,bottomMargin=18) 
    elements = [] 
    datas = [] 
    for x in range(1,50): 
     datas.append(
      [x,x+1] 
     ) 
    t=Table(datas) 
    tTableStyle=[ 
     ('SPAN',(0,0),(0,37)), 
     ] 
    t.setStyle(TableStyle(tTableStyle)) 
    elements.append(t) 
    doc.build(elements) 

if __name__ == '__main__': 
    testPdf() 

此代碼運行的成功,因爲該表是在一個頁面上,如果我設置 「SPAN」 到「(0,0),(0,38 )」,錯誤的是:

reportlab.platypus.doctemplate.LayoutError: Flowable with cell(0,0) containing
'1'(46.24 x 702) too large on page 2 in frame 'normal'(456.0 x 690.0*) of template 'Later'

,如果我把它更大的錯誤是:

Traceback (most recent call last): 
    File "testpdf.py", line 26, in <module> 
    testPdf() 
    File "testpdf.py", line 23, in testPdf 
    doc.build(elements) 
    File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py", line 1117, in build 
    BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker) 
    File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py", line 880, in build 
    self.handle_flowable(flowables) 
    File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py", line 763, in handle_flowable 
    if frame.add(f, canv, trySplit=self.allowSplitting): 
    File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/frames.py", line 159, in _add 
    w, h = flowable.wrap(aW, h) 
    File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py", line 1113, in wrap 
    self._calc(availWidth, availHeight) 
    File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py", line 587, in _calc 
    self._calc_height(availHeight,availWidth,W=W) 
    File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py", line 553, in _calc_height 
    spanFixDim(H0,H,spanCons,lim=hmax) 
    File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py", line 205, in spanFixDim 
    t = sum([V[x]+M.get(x,0) for x in xrange(x0,x1)]) 
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' 

我該如何面對呢?

+1

你想看什麼行爲?你得到這個錯誤的原因正是你期望的:表單元太大而不能在單個框架中顯示。您需要以某種方式使框架變大或拆分單元格。 –

+0

有沒有什麼辦法可以自動在頁面之間製作「SPAN」? – Danfi

+0

不是我所知道的。我敢肯定,從根本上來說,這與渲染單元的工作方式有關。 –

回答

1

你面對這個問題的原因正是Gordon Worley評論過的。沒有辦法跨頁面自動跨越,因爲執行的算法會混淆計算的高度和寬度。

解決此問題的一種方法是使用行/列座標手動爲每個頁面設置表格/樣式。可悲的是,即使在reportlab中的回覆也表明我們手動執行此操作。

我的確手動分割了我的表格,並將它們分開設計,這在我看來是一種非常難看的方法。稍後我會尋找其他替代方案。

僅供參考:https://bitbucket.org/ntj/reportlab_imko_table