gpt4 book ai didi

python - TableFactory PDFTable 仅输出列标题

转载 作者:太空宇宙 更新时间:2023-11-03 16:22:52 26 4
gpt4 key购买 nike

我正在尝试使用 TableFactory 创建一个表,但几乎没有文档,只有 two examples我能找到的。 PDFTable 函数确实输出了 PDF,但它似乎没有找到数据!

我的代码:

#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

最佳答案

你已经很接近了!以下是我稍微修改一下的方法:

#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 需要一个行对象列表,而不是列列表。我将您的列列表(idslonlat深度)转换为 TableFactory 的 DataRow 对象列表可以咀嚼。

关于python - TableFactory PDFTable 仅输出列标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38252485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com