Reputation: 13
I am trying to use Python to create multiple pdf files each containing 3 tables (each with text wrapping within the cells as well as a title at the top of the table). I am having difficulty finding the perfect library to use.
The closest I have got is using the fpdf "multi_cell" function, and using a tutorial I got a script that looks like this:
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')
This solution is really close to what I want because it has text wrapping and it allows me to put a title above the table. But I can't find any documentation on how to set the cell height automatically. In this example the two cell heights turn out to be different in the pdf because of the amount of text inside each one. I will be using this script to make many pdfs with unpredictable amounts of text in the cells of each table, so I would need the height to be determined automatically... Also whenever I try to add more rows to this table they just overlap with each other.
Anyone know know how to fix these 2 problems or any other way to get multiple tables that fit these conditions into a pdf? (Note: I am on Linux so I cannot create a docx first and convert it to pdf).
Upvotes: 0
Views: 2099
Reputation: 11
def alarms_summary(self):
PDF .set_font("Arial", size=8, style="B")
PDF .cell(180, 4, 'ALARMS SUMMARY', 1, 0, 'L')
PDF .Ln()
alarms_summary_col_names = ["Alarm Time", "Description"]
PDF .set_font("Arial", size=6, style="B")
for col_name in range(Len(alarms_summary_col_names)):
PDF .cell(30*3, 4*1, 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=['Low Air Pressure','LAP Overflow Station 1','LAP Overflow Station 1']
alarms_summary=list(zip(Alarm_Time_col_values,Description_col_values))
PDF .set_font("Arial", size=6)
# alarms_summary = cur .fetch all()
retrieved_pg_no = PDF .page_no()
for alarm in alarms_summary:
# print(alarm)
for value in [alarm[1], alarm[0]]:
# print(PDF .page_no())
if PDF .page_no() == retrieved_pg_no:
PDF .cell(30*3, 4*1, string(value), 1,0, 'L')
else:
PDF .Ln()
count = 1.5
for col_name in range(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)
Upvotes: 1