fpdf 库的 FPDF 类没有属性“table”

问题描述 投票:0回答:2

我正在尝试使用 FPDF 库在 Python 中创建一个表。以下代码给我一个错误:

'FPDF' object has no attribute 'table'.
我不知道如何解决它。我将感谢您的帮助。

代码:

import fpdf
from fpdf import FPDF

pdf = FPDF()

TABLE_Data = (('Etap' , 'Czas rozpoczecia' , 'Czas trwania'),
                  ('Wychladzanie pieca' , 'IDLE_start' , ''),
                  ('NIF temp = 1200C' , 'IDLE_step1' , 'IDLE_time_step1'),
                  ('Przycisk DROP' , 'IDLE_step2' , 'IDLE_time_step2'),
                  ('DROP' , 'IDLE_step3' , 'IDLE_time_step3'),
                  ('Preciki - START' , 'IDLE_step4' , 'IDLE_time_step4'),
                  ('Preciki - STOP' , 'IDLE_step5' , 'IDLE_time_step5'),
                  ('V = 15 m/min' , 'IDLE_step6' , 'IDLE_time_step6'),
                  ('V = 50 m/min' , 'IDLE_step7' , 'IDLE_time_step7'),
                  ('Srednica w tolerancji' , 'IDLE_step8' , 'IDLE_time_good'),
                  ('Predkosc robocza' , 'IDLE_end' , 'IDLE_time_end')
                  )
pdf.add_page()

pdf.set_font('Helvetica', 'b', 18) 

with pdf.table() as table:
    for data_row in TABLE_Data:
        row = table.row()
        for datum in data_row:
            row.cell(datum)


pdf.output('table.pdf')

我在论坛上寻找答案。唯一的信息是关于:pdf.add_page() 和 pdf.set_font(),但这两个选项都在我的示例中声明。

python pyfpdf
2个回答
0
投票

根据fpdf,

table
方法不存在

如果您的目标是创建这样的表

enter image description here

你必须手动绘制表格结构

例如:

手动创建包含您需要的所有方法的类并使用它们。将 FPDF 传递给班级


0
投票

FPDF
的类
fpdf
没有任何属性
table
,所以错误:

AttributeError: 'FPDF' object has no attribute 'table'

很明显。

将元组数据写入 PDF 文件内的表格中的有用代码如下:

TABLE_Data

一些注意事项:

我已将您的
    import fpdf from fpdf import FPDF pdf = FPDF() TABLE_Data = (('Etap' , 'Czas rozpoczecia' , 'Czas trwania'), ('Wychladzanie pieca' , 'IDLE_start' , ''), ('NIF temp = 1200C' , 'IDLE_step1' , 'IDLE_time_step1'), ('Przycisk DROP' , 'IDLE_step2' , 'IDLE_time_step2'), ('DROP' , 'IDLE_step3' , 'IDLE_time_step3'), ('Preciki - START' , 'IDLE_step4' , 'IDLE_time_step4'), ('Preciki - STOP' , 'IDLE_step5' , 'IDLE_time_step5'), ('V = 15 m/min' , 'IDLE_step6' , 'IDLE_time_step6'), ('V = 50 m/min' , 'IDLE_step7' , 'IDLE_time_step7'), ('Srednica w tolerancji' , 'IDLE_step8' , 'IDLE_time_good'), ('Predkosc robocza' , 'IDLE_end' , 'IDLE_time_end') ) pdf.add_page() pdf.ln(10) pdf.set_font('Helvetica', 'b', 15) for data_row in TABLE_Data: for data in data_row: pdf.cell(w=60, h=10, txt=str(data), border=1) pdf.ln() pdf.output('table.pdf')
  • 字符字体减小为
    Helvetica
    ,因为
    15
    太大了;
    指令 
  • 18
  • pdf.ln(10)
    中作为参数出现的数字以毫米为单位,因为毫米是使用的默认单位。
    
    
  • 文件
pdf.cell(w=60, h=10, txt=str(data), border=1)

内的表格如下所示:

table inside the pdf
    
	

© www.soinside.com 2019 - 2024. All rights reserved.