【问题标题】:Python - Creating a pdf with tables and table titlesPython - 创建带有表格和表格标题的 pdf
【发布时间】:2022-11-10 17:54:58
【问题描述】:

我正在尝试使用 Python 创建多个 pdf 文件,每个文件包含 3 个表格(每个表格都包含在单元格内的文本以及表格顶部的标题)。我很难找到要使用的完美库。

我得到的最接近的是使用 fpdf "multi_cell" 函数,并使用教程我得到了一个如下所示的脚本:

from fpdf import FPDF

pdf=FPDF(format='letter', unit='in')

pdf.add_page()

pdf.set_font('helvetica','',10.0)

loremipsum_1 = """Lorem ipsum dolor sit amet, vel ne quando dissentias. \
Ne his oporteat expetendis. Ei tantas explicari quo, sea vidit minimum \
menandri ea. His case errem dicam ex, mel eruditi tibique delicatissimi ut. \
At mea wisi dolorum contentiones, in malis vitae viderer mel.
"""

loremipsum_2 = """Vis at dolores ocurreret splendide. Noster dolorum repudiare \
vis ei, te augue summo vis. An vim quas torquatos, electram posidonium eam ea, \
eros blandit ea vel. Reque summo assueverit an sit. Sed nibh conceptam cu, pro \
in graeci ancillae constituto, eam eu oratio soleat instructior. No deleniti \
quaerendum vim, assum saepe munere ea vis, te tale tempor sit. An sed debet ocurreret \
adversarium, ne enim docendi mandamus sea.
"""

effective_page_width = pdf.w - 2*pdf.l_margin

pdf.multi_cell(effective_page_width, 0.15, "Test title")
pdf.ln(0.5)

ybefore = pdf.get_y()
pdf.multi_cell(effective_page_width/2, 0.15, loremipsum_1, 1)


pdf.set_xy(effective_page_width/2 + pdf.l_margin, ybefore)
pdf.multi_cell(effective_page_width/2, 0.15, loremipsum_2, 1)
pdf.ln(0.5)

pdf.output('multi_cell_adjacent.pdf','F')

这个解决方案非常接近我想要的,因为它有文本换行,它允许我在表格上方放置一个标题。但我找不到任何关于如何自动设置单元格高度.在此示例中,由于每个单元格内部的文本数量,两个单元格的高度在 pdf 中是不同的。我将使用此脚本制作许多 pdf,每个表格的单元格中的文本数量不可预测,因此我需要自动确定高度......而且每当我尝试添加更多行在这张桌子上,它们只是相互重叠。

任何人都知道如何解决这两个问题或任何其他方式将符合这些条件的多个表格放入 pdf 中? (注意:我在 Linux 上,所以我不能先创建一个 docx 并将其转换为 pdf)。

太感谢了!

【问题讨论】:

    标签: python python-3.x pdf fpdf


    【解决方案1】:

    def alarms_summary(self): PDF .set_font("Arial", size=8, style="B") PDF .cell(180, 4, '警报摘要', 1, 0, 'L') PDF .Ln() alarms_summary_col_names = [“闹钟时间”,“描述”] PDF .set_font("Arial", size=6, style="B") 对于范围内的 col_name(Len(alarms_summary_col_names)): PDF .cell(303、41, alarms_summary_col_names[col_name], 1,0, 'L') PDF .Ln() Alarm_Time_col_values=['22/06/2017 07:26:04','22/06/2017 08:50:26','22/06/2017 11:13:33'] Description_col_values=['低气压','LAP 溢流站 1','LAP 溢流站 1'] alarms_summary=list(zip(Alarm_Time_col_values,Description_col_values)) PDF .set_font("Arial", size=6) # alarms_summary = cur .fetch all() 检索的_pg_no = PDF .page_no() 对于alarms_summary 中的警报: # 打印(报警) 对于 [alarm[1], alarm[0]] 中的值: # 打印(PDF .page_no())
    如果 PDF .page_no() == retrieved_pg_no: PDF .cell(303、41,字符串(值),1,0,'L') 别的: PDF .Ln() 计数 = 1.5 对于范围内的 col_name(Len(alarms_summary_col_names)):

                        PDF .cell(30*3, 4*1, alarms_summary_col_names[col_name], 1,0, 'L')
                        count = 1.5
                    PDF .Ln()
                    PDF .cell(30*3, 4*1, Str(value), 1,0, 'L')
                    retrieved_pg_no = PDF .page_no()
            PDF .Ln()
        PDF .Ln(6)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 2019-03-05
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2019-08-31
      相关资源
      最近更新 更多