How do I use Pandas for reading multiple xlsx files and outputting into one in individual file in multiple sheets? [closed]

那年仲夏 提交于 2019-12-04 22:05:49

The process for doing this is:

0. Setup

Install required packages:

pip install pandas
pip install xlsxwriter

Then import pandas into the Python file you're working in:

import pandas as pd

1. Read in the .xlsx files

a. Each by name:

df1 = pd.read_excel('./excelfile1.xlsx')

etc

b. Read all in current directory in:

import os, re
dfs = []
for fname in os.listdir():
    if re.search(r'\.xlsx$', fname):
        dfs.append(pd.read_excel(fname))

2. Create a new file and add existing files as sheets

writer = pd.ExcelWriter('./newfilename.xlsx', engine='xlsxwriter')
sheet_names = ['sheet1', ...]
for df, sheet_name in zip(dfs, sheet_names):
    df.to_excel(writer, sheet_name=sheet_name)
writer.save()

This will create a new Excel file in the current directory called newfilename.xlsx with each of your existing Excel files as sheets.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!