且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python在多个Excel中搜索对应的数据并粘贴到新的Excel工作表中

更新时间:2022-12-12 08:12:23

使用pandas更加容易操作,我相信它仍然会在幕后使用openpyxl.

Use pandas its much easier to manipulate, I believe it uses openpyxl under the hood anyway.

import glob
import pandas as pd
import os


for f in glob.glob('Notes_*.xlsx'):
    dda = re.findall('\d+', f) #matches digits in the filename

    df_each = pd.read_excel(f) # have to save the data first, coz ExcelWriter will clear up and create a new excel, so, you paste the saved data back to new sheet
    df_1_dda = df_master[df_master['code'] == int(dda[0])] #select only those records with code in the filename

    writer = pd.ExcelWriter(f)
    df_each.to_excel(writer, 'service', index = False) #  paste the saved data back to new sheet
    df_1_dda.to_excel(writer, 'code_city', index = False)
    writer.close()

希望有帮助!

使用python 3.6.4 Anaconda-32位

using python 3.6.4 Anaconda - 32-bit