Python教程

python fpdf插入表格

本文主要是介绍python fpdf插入表格,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# Import FPDF class
from fpdf import FPDF

# Create instance of FPDF class
# Letter size paper, use inches as unit of measure
pdf=FPDF(format='letter', unit='in')

# Add new page. Without this you cannot create the document.
pdf.add_page()

# Remember to always put one of these at least once.
pdf.set_font('Times','',10.0)

# Effective page width, or just epw
epw = pdf.w - 2*pdf.l_margin

# Set column width to 1/4 of effective page width to distribute content
# evenly across table and page
col_width = epw/4

# Since we do not need to draw lines anymore, there is no need to separate
# headers from data matrix.

data = [['First name','Last name','Age','City'],
['Jules','Smith',34,'San Juan'],
['Mary','Ramos',45,'Orlando'],[
'Carlson','Banks',19,'Los Angeles']
]

# Document title centered, 'B'old, 14 pt
pdf.set_font('Times','B',14.0)
pdf.cell(epw, 0.0, 'Demographic data', align='C')
pdf.set_font('Times','',10.0)
pdf.ln(0.5)

# Text height is the same as current font size
th = pdf.font_size

for row in data:
    for datum in row:
        # Enter data in colums
        # Notice the use of the function str to coerce any input to the
        # string type. This is needed
        # since pyFPDF expects a string, not a number.
        pdf.cell(col_width, th, str(datum), border=1)

    pdf.ln(th)

# Line break equivalent to 4 lines
pdf.ln(4*th)

pdf.set_font('Times','B',14.0) 
pdf.cell(epw, 0.0, 'With more padding', align='C')
pdf.set_font('Times','',10.0)
pdf.ln(0.5)

# Here we add more padding by passing 2*th as height
for row in data:
    for datum in row:
        # Enter data in colums
        pdf.cell(col_width, 2*th, str(datum), border=1)

    pdf.ln(2*th)

pdf.output('table-using-cell-borders.pdf','F')

在这里插入图片描述

有空请博主喝杯咖啡吗
在这里插入图片描述

这篇关于python fpdf插入表格的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!