2016-07-07 18 views
1

我試圖用TableFactory創建一個表,但幾乎沒有文檔,只有twoexamples,我可以找到。 PDFTable函數確實輸出PDF,但它似乎沒有找到數據!TableFactory PDFTable只輸出列標題

我的代碼:

#Import module components 
from TableFactory import * 

# Build the data setup required 
ids = ['a','b','c','d','e','f','g','h','i'] 

lon = [-80.15765381, -80.40131378, -80.56086731, -80.52893066, 
     -80.24595642, -80.7718277, -80.877388, -79.9454422, -79.84288025] 

lat = [ 0.34739658, -0.39966396, -0.91119879, 0.83061123, 0.67057306, 
     -0.1843673, -0.18189907, 0.43762371, 0.45526683] 

depth = [ 14044, 4942, 7000, 13107, 
     6281, 7000, 1172, 4825, 6730] 

rows4 = TableRow() 
setattr(rows4, 'ids', ids) 
setattr(rows4, 'lon', lon) 
setattr(rows4, 'lat', lat) 
setattr(rows4, 'depth', depth) 


invoicerow = RowSpec(ColumnSpec('ids', 'Event ID'), 
        ColumnSpec('lon', 'Longitude'), 
        ColumnSpec('lat', 'Latitude'), 
        ColumnSpec('depth', 'Depth')) 

lines = invoicerow.makeall(rows4) 

#create the tables 
#HTML 
HTMLTable('Invoices by Customer', 
      'Amount of each invoice, sorted by invoiceid', 
      headers=invoicerow).render(lines) 

# Excel 
SpreadsheetTable('Invoices by Customer', 
       'Amount of each invoice, sorted by invoiceid', 
       headers=invoicerow).render(lines) 

# PDF 
pdfmaker = PDFTable('My title', 
        headers=invoicerow) 

open('invoicetable.pdf', 'wb').write(pdfmaker.render(lines)) 

但它不會產生什麼,我本來期望:

enter image description here

+0

作爲一個說明:我做了一個非常大的編輯,因爲我解決了最初的事情,我認爲是問題,但事實證明有一個不同的基礎。編輯轉移問題焦點 – mjp

回答

1

你是相當接近!下面是我如何重新修改它的方法:

#Import module components 
from collections import namedtuple 

from TableFactory import * 

# Build the data setup required 
ids = ['a','b','c','d','e','f','g','h','i'] 

lon = [-80.15765381, -80.40131378, -80.56086731, -80.52893066, 
     -80.24595642, -80.7718277 , -80.877388 , -79.9454422 , -79.84288025] 

lat = [ 0.34739658, -0.39966396, -0.91119879, 0.83061123, 0.67057306, 
     -0.1843673 , -0.18189907, 0.43762371, 0.45526683] 

depth = [ 14044.31152 , 4942.527294, 7000.  , 13107.94449 , 
     6281.775475, 7000.  , 1172.017574, 4825.51527 , 
     6730.996132] 

# Make an object that can represent a row in the table. This could be 
# a full-blown class, or a dict like {'id': 'a', 'lon': 0.3, ...}, etc. 
DataRow = namedtuple('DataRow', ['id', 'lon', 'lat', 'depth']) 

# Combine the separate lists together into a list of DataRow objects 
rows = [DataRow(*_) for _ in zip(ids, lon, lat, depth)] 

invoicerow = RowSpec(ColumnSpec('id', 'Event ID'), 
        ColumnSpec('lon', 'Longitude'), 
        ColumnSpec('lat', 'Latitude'), 
        ColumnSpec('depth', 'Depth')) 

lines = invoicerow.makeall(rows) 

#create the tables 
#HTML 
html = HTMLTable('Invoices by Customer', 
       'Amount of each invoice, sorted by invoiceid', 
       headers=invoicerow).render(lines) 

# Excel 
excel = SpreadsheetTable('Invoices by Customer', 
         'Amount of each invoice, sorted by invoiceid', 
         headers=invoicerow).render(lines) 

# PDF 
pdf = PDFTable('Invoices by Customer', 
       'Amount of each invoice, sorted by invoiceid', 
       headers=invoicerow).render(lines) 

with open('my.pdf', 'wb') as outfile: 
    outfile.write(pdf) 

基本上,TableFactory需要一個行對象列表,而不是列列表。我將列列表(ids,lon,lat,depth)轉換爲TableFactory可以咀嚼的DataRow對象列表。

+0

謝謝!我在編寫這個問題時編輯了這個問題,但它仍然解決了這個問題,現在我得到了一個完整的pdf輸出。它似乎每次渲染PDF時都會崩潰GUI('Spyder'),但我不確定爲什麼! – mjp

+0

它會在SpreadsheetTable和PDFTable兩行上崩潰,迫使我重新啓動,但它仍然會生成完整的PDF。這臺電腦變得有點老了,所以它可能因爲內存需求等原因而凍結。 – mjp