简体   繁体   中英

Pythn split an excel sheet to multiple pdf files

I have a single excel worksheet that I would like to split into multiple pdf files, using python. To save to multiple excel files I have used a code like below, but not sure how to modify it for a pdf output. Any ideas?

import pandas as pd
data_df = pd.read_excel('./data_1.xlsx')
grouped_df = data_df.groupby('columnA')

for data in grouped_df.displayName:
    grouped_df.get_group(data[0]).to_excel("./IO/Files/"+data[0]+".xlsx")

This can be done using matplotlib:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

data_df = pd.read_excel('./data_1.xlsx')
grouped_df = data_df.groupby('columnA')

for data in grouped_df.displayName:
    temp_df = grouped_df.get_group(data[0])
    temp_df.to_excel("./IO/Files/"+data[0]+".xlsx")
    
    fig, ax = plt.subplots(figsize=(12,4))
    ax.axis('tight')
    ax.axis('off')
    ax.table(cellText=tmp_df.values,colLabels=tmp_df.columns,loc='center')
    pp = PdfPages("./IO/Files/"+ data[0] + ".pdf")
    pp.savefig(fig, bbox_inches='tight')
    pp.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM