我需要使用 Python xlwings、Pandas、numpy 将一个 xlsx 文件中的数据填充到另一个 xlsm 文件中……我是 Python 的新手

I need to fill the data in one xlsx file to another xlsm file using Python xlwings, Pandas, numpy ... I am new to Python

我是 Python 的新手。我想自动执行手动 Excel 任务。请帮助我。

我有一个主 xlsm 文件和另一个包含多张纸的 xlsx 文件。

我正在从 xlsx 文件中的多个工作表中获取值并在 xlsm 文件中填充所需的单元格。

主 xlsm 文件包含:

Item no, Item Name, Jan              
10,       aaa,      data_to_be_filled
20,       bbb,      data_to_be_filled
30,       ccc,      data_to_be_filled

源 xlsx 文件:

Item no, Item Name, Amount
10,       aaaa       1000
20,       bbbb       2000
30,       cccc       3000

我要在主文件'Jan'列的相应单元格中填写金额列数据。

编辑,我尝试了以下方法:

pd.read_excel. df1 = pd.read_excel(master_file, sheet_name='Sheet1', header=52) 
df2 = pd.read_excel(source_file, sheet_name='Sheet1', header=2)
Left_join = pd.merge(df1, df2, on ='Item no.', how ='left') 
right_join = pd.merge(df1, df2, on = 'Item no.', how='right') 
left_right_join = pd.merge(df1, df2, left_on = 'Item no.', right_on='Item no.', how='right') 
inner_join = pd.merge(df1, df2, on = 'Item no.', how='inner') 
outer_join = pd.merge(df1, df2, on='Item no.', how='outer')

这是你想要的吗?

df_final = pd.merge(df1, df2[["Item no", "Amount"]], on="Item no", how="left")
df_final["Jan"] = df_final["Amount"]
df_final.drop("Amount", axis=1, inplace=True)

输出:

    Item no Item Name   Jan
0   10      aaa         1000
1   20      bbb         2000
2   30      ccc         3000

感谢您的回答。

我用下面的代码解决了。

list_item_no = [10,20,30...]
x = 0
y = 0

def item_no_in_source_file():
    for item_no in list_item_no:
        for row_source in range(1,55):
            for col_source in range(1, 10):
                if ws2.range((row_source,col_source)).value == item_no:                                   
                    ns_col = 2  
                    global x, y
                    x = item_no
                    y = (int(round(ws2.range(row_source, ns_col).value)))
                    item_no_in_master_file(x, y)
                else:
                    continue


def item_no_in_master_file(a, b):    
        for row in range(1, 200): 
            for col in range(0, 2):                
                if ws1.range((row,col)).value == a:
                    jan_col = 2
                    ws1.range(row, jan_col).value = y
                else:
                    continue


item_no_in_source_file()