1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
from fpdf import FPDF
import pandas as pd
DF = pd.DataFrame(
{
"First name": ["Jules", "Mary", "Carlson", "Lucas"],
"Last name": ["Smith", "Ramos", "Banks", "Cimon"],
"Age": [34, 45, 19, 31],
"City": ["San Juan", "Orlando", "Los Angeles", "Saint-Mathurin-sur-Loire"],
}
# Convert all data inside dataframe into string type:
).applymap(str)
COLUMNS = [list(DF)] # Get list of dataframe columns
ROWS = DF.values.tolist() # Get list of dataframe rows
DATA = COLUMNS + ROWS # Combine columns and rows in one list
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=10)
with pdf.table(
borders_layout="MINIMAL",
cell_fill_color=200, # grey
cell_fill_mode="ROWS",
line_height=pdf.font_size * 2.5,
text_align="CENTER",
width=160,
) as table:
for data_row in DATA:
row = table.row()
for datum in data_row:
row.cell(datum)
pdf.output("table_from_pandas.pdf")
|