Last active
March 16, 2024 10:42
-
-
Save lakshmanok/3321e16a266b24be1428dc59a8f94c9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.backends.backend_pdf import PdfPages | |
def _draw_as_table(df, pagesize): | |
alternating_colors = [['white'] * len(df.columns), ['lightgray'] * len(df.columns)] * len(df) | |
alternating_colors = alternating_colors[:len(df)] | |
fig, ax = plt.subplots(figsize=pagesize) | |
ax.axis('tight') | |
ax.axis('off') | |
the_table = ax.table(cellText=df.values, | |
rowLabels=df.index, | |
colLabels=df.columns, | |
rowColours=['lightblue']*len(df), | |
colColours=['lightblue']*len(df.columns), | |
cellColours=alternating_colors, | |
loc='center') | |
return fig | |
def dataframe_to_pdf(df, filename, numpages=(1, 1), pagesize=(11, 8.5)): | |
with PdfPages(filename) as pdf: | |
nh, nv = numpages | |
rows_per_page = len(df) // nh | |
cols_per_page = len(df.columns) // nv | |
for i in range(0, nh): | |
for j in range(0, nv): | |
page = df.iloc[(i*rows_per_page):min((i+1)*rows_per_page, len(df)), | |
(j*cols_per_page):min((j+1)*cols_per_page, len(df.columns))] | |
fig = _draw_as_table(page, pagesize) | |
if nh > 1 or nv > 1: | |
# Add a part/page number at bottom-center of page | |
fig.text(0.5, 0.5/pagesize[0], | |
"Part-{}x{}: Page-{}".format(i+1, j+1, i*nv + j + 1), | |
ha='center', fontsize=8) | |
pdf.savefig(fig, bbox_inches='tight') | |
plt.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment