我正在制作一张由大约10个带有标题的单元格组成的表格。除非我使用multi_cell选项,否则它们不会适合整个页面。但是,我不知道如何并行获得一个multi_cell。当我做一个新的,它自动转到下一行。
from fpdf import FPDF
import webbrowser
pdf=FPDF()
pdf.add_page()
pdf.set_font('Arial','B',16)
pdf.multi_cell(40,10,'Hello World!,how are you today',1,0)
pdf.multi_cell(100,10,'This cell needs to beside the other',1,0)
pdf.output('tuto1.pdf','F')
webbrowser.open_new('tuto1.pdf')发布于 2016-02-20 18:17:42
您必须跟踪x和y坐标:
from fpdf import FPDF
import webbrowser
pdf=FPDF()
pdf.add_page()
pdf.set_font('Arial','B',16)
# Save top coordinate
top = pdf.y
# Calculate x position of next cell
offset = pdf.x + 40
pdf.multi_cell(40,10,'Hello World!,how are you today',1,0)
# Reset y coordinate
pdf.y = top
# Move to computed offset
pdf.x = offset
pdf.multi_cell(100,10,'This cell needs to beside the other',1,0)
pdf.output('tuto1.pdf','F')
webbrowser.open_new('tuto1.pdf')发布于 2020-06-17 10:15:28
注:这是解决上述问题的另一种方法。
对于我的需求,我需要一些列具有较高的列宽和一些列具有较低的宽度。所有列的固定列宽度使我的表超出pdf边距。多个单元格包装文本,并且只对某些列而不是每一列增加列的高度。因此,我的方法是使用枚举函数并根据需要动态地调整列的宽度,如下所示。
data1 = list(csv.reader(csvfile))
print(data1) ##[[row1],[row2],[row3],[row4]]
## row1, row2, etc are the rows from csv
for row in data1:
for x,y in enumerate(row):
if (x == 0) or (x == 1): ## dynamically change the column width with certain conditions
pdf.cell(2.0, 0.15, str(y), align = 'C', border=1) ## width = 2.0
else:
pdf.cell(0.60, 0.15, str(y), align = 'L', border=1) ## width = 0.60希望这能有所帮助。
https://stackoverflow.com/questions/34921046
复制相似问题