Trying to create a PDF page using fpdf module. Not able add cells in a single row using multicell function. Below is the code that I am trying to use. Any suggestions for code change would be appreciated to get the expected output. Thanks in advance.
from fpdf import FPDF
import pandas as pd
class Tables(FPDF):
def table(self):
try:
self.add_page()
self.set_font('Times', '', 10.0)
epw = pdf.w - pdf.l_margin
col_width = epw/5
data1 = [{'ip': '192.168.0.1', 'name': 'cloud', 'used_percent': 100.0,'percent': 100.0,'value':6497693752875},
{'ip': '192.168.0.2', 'name': 'mitakaDZGFGASSCCGSTHAG', 'used_percent': 97.0, 'percent': 100.0, 'value':6497693752875597265917},
{'ip': '192.168.0.3', 'name': 'cloud', 'used_percent': 100.0,'percent': 100.0,'value':6497693752875},
{'ip': '192.168.0.4', 'name': 'cloud', 'used_percent': 100.0,'percent': 100.0,'value':6497693752875}]
df = pd.DataFrame(data1)
s = df['ip']
v = df['name']
c = df['used_percent']
d = df['percent']
e = df['value']
data = [df,[s[0], v[0], c[0], d[0],e[0]], [s[1], v[1], c[1], d[1], e[1]], [s[2], v[2], c[2], d[2], e[2]], [s[3], v[3], c[3], d[3], e[3]]]
th = pdf.font_size
count = 0
self.set_text_color(0, 0, 0)
self.set_fill_color(255, 255, 255)
for row in data:
count += 1
for datum in row:
if count == 1:
self.set_fill_color(70, 130, 180)
self.set_font('Times', '', 10)
self.cell(col_width, 2*th, str(datum), border=1, align='L', fill=1)
else:
self.set_font('Times', '', 11)
self.multi_cell(col_width, 2*th, str(datum), border=1, align='L', fill=0)
self.ln(2 * th)
self.ln(2* th)
except Exception as e:
print(f"Exception {e}")
pdf = Tables()
pdf.table()
pdf.output(‘fpdf_table.pdf’, ‘F’)