我下面有DataFrame:
问题是,当我尝试使用summary_debit
(即DataFrame
)制作表格时,会出现以下错误:ValueError: <Table@0x2231B045208 unknown rows x unknown cols>... invalid data type
from reportlab.pdfgen import canvas
from reportlab.platypus import *
from reportlab.lib import colors
colwidths = 50
GRID_STYLE = TableStyle(
[('GRID', (0, 0), (-1, -1), 0.25, colors.pink),
('ALIGN', (1, 0), (-1, -1), 'RIGHT')])
t1 = Table(summary_debit)
答案 0 :(得分:5)
我为您的DataFrame图像构建了一个示例,但是您可以对其进行扩展。
运行代码后,我得到了正确的pdf。
希望这段代码对您有所帮助。
from reportlab.pdfgen import canvas
from reportlab.platypus import *
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
import pandas as pd
import numpy as np
data = {'Account Name': ['ACCOUNT PAYABLE', 'PAGIBIG LOAN PAYABLE','PREPAID TAX']
,'': [-0.1, -0.2,-0.3]}
summary_debit = pd.DataFrame(data=data)
colwidths = 50
GRID_STYLE = TableStyle(
[('GRID', (0, 0), (-1, -1), 0.25, colors.pink),
('ALIGN', (1, 0), (-1, -1), 'RIGHT')])
#t1 = Table([summary_debit.iloc[:,1].tolist(),summary_debit.iloc[:,0].tolist()]);
t1 = Table(np.array(summary_debit).tolist());
doc = SimpleDocTemplate("table.pdf", pagesize=letter)
element = []
element.append(t1)
doc.build(element)
PDF图片导出
答案 1 :(得分:0)
(没有足够的声誉来评论米菲的问题)
要在表中显示数据框列名,您只需执行以下操作:
t1 = Table(np.vstack((list(summary_debit), np.array(summary_debit))).tolist());