2016-09-17 86 views
2

我正在設計一個雙面打印雙面打印。之後,我添加分頁符,我得到以下錯誤:分頁符後出現Reportlab錯誤

File "f:\Dropbox\pms\pms_reports.py", line 450, in <module> 
    a = Key_card1() 
File "f:\Dropbox\pms\pms_reports.py", line 441, in __init__ 
    doc.build(elements) 
File "c:\Python34\Lib\site-packages\reportlab\platypus\doctemplate.py", line 1171, in build 
    BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker) 
File "c:\Python34\Lib\site-packages\reportlab\platypus\doctemplate.py", line 927, in build 
    self.handle_flowable(flowables) 
File "c:\Python34\Lib\site-packages\reportlab\platypus\doctemplate.py", line 775, in handle_flowable 
    self.handle_keepWithNext(flowables) 
File "c:\Python34\Lib\site-packages\reportlab\platypus\doctemplate.py", line 742, in handle_keepWithNext 
    while i<n and flowables[i].getKeepWithNext(): i += 1 

builtins.TypeError: getKeepWithNext() missing 1 required positional argument: 'self' 

這裏是我的代碼:

from reportlab.lib.pagesizes import A4, landscape 
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, PageBreak 
from reportlab.lib.styles import getSampleStyleSheet 

    class Key_card1(): 
    def __init__(self, start_date=datetime.now(), room="1", 
      end_date=datetime.now()+timedelta(days=1), password = "999999"): 
     stylesheet = getSampleStyleSheet() 
     doc = SimpleDocTemplate("key_card.pdf", pagesize=A4) 

     if password == "999999": 
      password = str(random.randint(10000,99999)) 

     roomno = "Room" + room 
     w_text = [] 
     w_text.append(Paragraph("To use the wireless broadband…:",stylesheet["BodyText"])) 
     w_text.append(Paragraph("User id: <b>" + roomno + "</b>", stylesheet["BodyText"])) 
     w_text.append(Paragraph("Password: <b>" + password + "</b>", stylesheet["BodyText"])) 

     message_text = [] 
     message_text.append(Paragraph("Our current menus ...", stylesheet["BodyText"])) 
     message_text.append(Paragraph("Our restaurant can get very busy ...",stylesheet["BodyText"])) 
     message_text.append(Paragraph("Your shower has a safety device to...", stylesheet["BodyText"])) 
     message_text .append(Paragraph("Please do not hesitate to call...",stylesheet["BodyText"])) 


     elements=[] 
     table_data = [(w_text, message_text)] 
     the_table = Table(table_data) 
     the_table.setStyle(TableStyle([('VALIGN',(0,0),(-1,-1),'MIDDLE')])) 
     elements.append(the_table) 
     elements.append(PageBreak) 

     #Cover page 
     logo= "y:\marketing\priory_master_logo bw.jpg" 
     im = Image(logo, 3*cm, 1.258*cm) 

     cover_data = [] 
     cover_data.append(im) 
     cover_data.append(Paragraph("Room number : " + room, stylesheet["BodyText"])) 
     left_cell =[] 
     left_cell.append(Paragraph(" ", stylesheet["BodyText"])) 


     table_data1 = [(left_cell, cover_data)] 
     the_table1 = Table(table_data1) 
     the_table1.setStyle(TableStyle([('VALIGN',(0,0),(-1,-1),'MIDDLE')])) 
     elements.append(the_table1) 


     doc.build(elements) 

我試圖修改ReportLab的模塊,但我沒有成功。任何人有任何想法?

回答

2

我試圖理解錯誤,我猜這裏有問題 doc = SimpleDocTemplate("key_card.pdf", pagesize=A4)。問題可能是參數「key_card.pdf」。我不知道

編輯:也許,BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)問題是關於它連接到doc.build元素(元素)

解決方案「懸浮劑」的說法 - > elements.append(分頁符)應該是元素。附加(PageBreak())

+0

我轉換了代碼創建一個pageTemplate和框架和使用BaseDocTemplate並得到完全相同的錯誤。當我將分頁符註釋掉時,它全部打印出來沒有錯誤(除非沒有分頁符)。 – Dkellygb

+0

我想我發現它'elements.append(PageBreak)'應該是'elements.append(PageBreak())'。我谷歌搜索「缺少1所需的位置參數:'自我'」,並發現這些[鏈接](http://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self)[鏈接]( http://stackoverflow.com/questions/37289391/missing-1-required-positional-argumentself-when-assigning-a-method-to-a-varia)你應該注意類初始化;) –

+0

你是絕對正確的。我不知道爲什麼我沒有看到它。如果你讓你的評論成爲答案,我會接受它。 - 謝謝! – Dkellygb