我已经使用 pyfpdf 模块生成了 PDF 文件,但我注意到每行中的文本未正确对齐。
我需要在单元格中进行文本换行才能自动调整。我尝试使用下面的代码生成固定大小的 PDF 文件,但它无法将文本混合在行中,您可以检查评论中的屏幕截图来查看问题。
我需要动态调整表列大小。请帮忙。
代码 :
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=8)
item_data = [['Item Name', 'Last clock', 'lastvalue'], ['cl Loaded Class Count', '0', '0'], ['cl Total Loaded Class Count', '0', '0'], ['cl Unloaded Class Count', '0', '0'], ['comp Name of the current JIT compiler', '0', ''], ['comp Accumulated time spent in compilation', '0', '0'], ['gc ConcurrentMarkSweep number of collections per second', '0', '0'], ['gc ConcurrentMarkSweep accumulated time spent in collection', '0', '0'], ['gc Copy number of collections per second', '0', '0'], ['gc Copy accumulated time spent in collection', '0', '0'], ['gc MarkSweepCompact number of collections per second', '0', '0'], ['gc MarkSweepCompact accumulated time spent in collection', '0', '0'], ['gc PS MarkSweep number of collections per second', '0', '0'], ['gc PS MarkSweep accumulated time spent in collection', '0', '0'], ['gc PS Scavenge number of collections per second', '0', '0'], ['gc PS Scavenge accumulated time spent in collection', '0', '0'], ['gc ParNew number of collections per second', '0', '0'], ['gc ParNew accumulated time spent in collection', '0', '0'], ['mem Heap Memory committed', '0', '0'], ['mem Heap Memory max', '0', '0'], ['mem Heap Memory used', '0', '0'], ['mem Non-Heap Memory committed', '0', '0'], ['mem Non-Heap Memory max', '0', '0'], ['mem Non-Heap Memory used', '0', '0'], ['mem Object Pending Finalization Count', '0', '0'], ['mp CMS Old Gen committed', '0', '0'], ['mp CMS Old Gen max', '0', '0'], ['mp CMS Old Gen used', '0', '0'], ['mp CMS Perm Gen committed', '0', '0'], ['mp CMS Perm Gen max', '0', '0'], ['mp CMS Perm Gen used', '0', '0'], ['mp Code Cache committed', '0', '0'], ['mp Code Cache max', '0', '0'], ['mp Code Cache used', '0', '0'], ['mp PS Old Gen committed', '0', '0'], ['mp PS Old Gen max', '0', '0'], ['mp PS Old Gen used', '0', '0'], ['mp PS Perm Gen committed', '0', '0'], ['mp PS Perm Gen max', '0', '0'], ['mp PS Perm Gen used', '0', '0'], ['mp Perm Gen committed', '0', '0'], ['mp Perm Gen max', '0', '0'], ['mp Perm Gen used', '0', '0'], ['mp Tenured Gen committed', '0', '0'], ['mp Tenured Gen max', '0', '0'], ['mp Tenured Gen used', '0', '0'], ['os Max File Descriptor Count', '0', '0'], ['os Open File Descriptor Count', '0', '0'], ['os Process CPU Load', '0', '0'], ['jvm Uptime', '0', '0'], ['jvm Name', '0', ''], ['jvm Version', '0', ''], ['th Daemon Thread Count', '0', '0'], ['th Peak Thread Count', '0', '0'], ['th Thread Count', '0', '0'], ['th Total Started Thread Count', '0', '0']]
col_width = pdf.w / 3.25
print(col_width)
row_height = pdf.font_size
for row in item_data:
for item in row:
spacing=1.5
pdf.cell(col_width, row_height*spacing,txt=item, border=1)
pdf.ln(row_height*spacing)
pdf.output("stack.pdf")
Run Code Online (Sandbox Code Playgroud)
使用andmulti_cell()代替cell()ln=3max_line_height=pdf.font_size
ln (int):指示调用后当前位置应该去哪里。可能的值有:
0: 位于右下角;1: 到下一行的开头;2:下方具有相同的水平偏移;3:向右,具有相同的垂直偏移。默认值:0。
max_line_height (int):可选生成的每个子单元的最大高度
下面是一个工作示例: 注意:我必须更改spacing = 3. 这需要根据单个单元格中的行数进行调整。
from fpdf import FPDF
def test_pdf():
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=8)
item_data = [['Item Name', 'Last clock', 'lastvalue'], ['cl Loaded Class Count', '0', '0'],
['cl Total Loaded Class Count', '0', '0'], ['cl Unloaded Class Count', '0', '0'],
['comp Name of the current JIT compiler', '0', ''],
['comp Accumulated time spent in compilation', '0', '0'],
['gc ConcurrentMarkSweep number of collections per second', '0', '0'],
['gc ConcurrentMarkSweep accumulated time spent in collection', '0', '0'],
['gc Copy number of collections per second', '0', '0'],
['gc Copy accumulated time spent in collection', '0', '0'],
['gc MarkSweepCompact number of collections per second', '0', '0'],
['gc MarkSweepCompact accumulated time spent in collection', '0', '0'],
['gc PS MarkSweep number of collections per second', '0', '0'],
['gc PS MarkSweep accumulated time spent in collection', '0', '0'],
['gc PS Scavenge number of collections per second', '0', '0'],
['gc PS Scavenge accumulated time spent in collection', '0', '0'],
['gc ParNew number of collections per second', '0', '0'],
['gc ParNew accumulated time spent in collection', '0', '0'], ['mem Heap Memory committed', '0', '0'],
['mem Heap Memory max', '0', '0'], ['mem Heap Memory used', '0', '0'],
['mem Non-Heap Memory committed', '0', '0'], ['mem Non-Heap Memory max', '0', '0'],
['mem Non-Heap Memory used', '0', '0'], ['mem Object Pending Finalization Count', '0', '0'],
['mp CMS Old Gen committed', '0', '0'], ['mp CMS Old Gen max', '0', '0'],
['mp CMS Old Gen used', '0', '0'], ['mp CMS Perm Gen committed', '0', '0'],
['mp CMS Perm Gen max', '0', '0'], ['mp CMS Perm Gen used', '0', '0'],
['mp Code Cache committed', '0', '0'], ['mp Code Cache max', '0', '0'],
['mp Code Cache used', '0', '0'], ['mp PS Old Gen committed', '0', '0'],
['mp PS Old Gen max', '0', '0'], ['mp PS Old Gen used', '0', '0'],
['mp PS Perm Gen committed', '0', '0'], ['mp PS Perm Gen max', '0', '0'],
['mp PS Perm Gen used', '0', '0'], ['mp Perm Gen committed', '0', '0'], ['mp Perm Gen max', '0', '0'],
['mp Perm Gen used', '0', '0'], ['mp Tenured Gen committed', '0', '0'],
['mp Tenured Gen max', '0', '0'], ['mp Tenured Gen used', '0', '0'],
['os Max File Descriptor Count', '0', '0'], ['os Open File Descriptor Count', '0', '0'],
['os Process CPU Load', '0', '0'], ['jvm Uptime', '0', '0'], ['jvm Name', '0', ''],
['jvm Version', '0', ''], ['th Daemon Thread Count', '0', '0'], ['th Peak Thread Count', '0', '0'],
['th Thread Count', '0', '0'], ['th Total Started Thread Count', '0', '0']]
col_width = pdf.w / 3.25
spacing = 3
print(col_width)
row_height = pdf.font_size
for row in item_data:
for item in row:
pdf.multi_cell(col_width, row_height * spacing, txt=item, border=1, ln=3, max_line_height=pdf.font_size)
pdf.ln(row_height * spacing)
pdf.output("stack1.pdf")
if __name__ == '__main__':
test_pdf()
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的问题,并在此处的答案中找到了解决方案使用 FPDF 将文本换行在单元格中?
注意:我找不到 FPDF 的 python 特定文档。我通常参考 FPDF 的 PHP 文档: http://www.fpdf.org/en/doc/multicell.htm http://www.fpdf.org/en/tutorial/tuto3.htm