简体   繁体   中英

Copy the data from specific files and pate it in master file on a specific sheet

I have a folder on my pc which I clean every monday and download new attachments (3 file in total for previous month current month and next month) from outlook (I have code for that).I have one master excel file with 12 sheet: January, February, March... December. The files I download from outlook has same names as sheet_name in master file. What I would like to do is this: I want to take a data from each outlook file and paste it on corresponding sheet. So if i have a file called December.xlsb I want to take all the data from sheet1 and paste it in master file on sheet_name December.

Master file and outlook attachments are in different directory. Preferably I would like to do it with pandas but I welcome to other solutions too.

Im not really sure how should I do or from where should I start. For sure I will need for loop and os.listdir I guess. Any help is appreciated

As you say, pandas is a good idea for this as it is good at manipulating excel files.

Start by reading the new excel files wherever they are saved. Then simply add the dataframe as a new sheet in the master excel workbook

import pandas as pd

month = input("Enter month: ")

new_file = f"C:\New Files\{month}.xlsb"
master_file = "C:\Master Files\master.xlsb"

df = pd.read_excel(new_file)

with pd.ExcelWriter(master_file, engine='openpyxl', mode='a') as writer:
    df.to_excel(writer,sheet_name=month)

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