使用 python 将 Excel 页导入一个 excel 文件

Import Excel sheets to one excel file using python

我已经尝试了很多方法将数据循环放入一个 excel sheet 和 xlsxwriter,pandas 等,但没有任何效果符合我的要求。所以我获得了很多数据。现在我正在为每个新的 'sampleid' 创建一个新的 excel sheet。然后我想把每一行都只包含数据(不是 headers bq/kg 等)并放入一个主 excel 文件中,所有 sheet 用于所有 'sampleid'.有没有办法重写这段代码使其循环,以便将新的 'sampleid' 放到相同的工作 sheet 中,但只降低一行? 编辑:我上传了 sheet 的照片This is how all the sheets looks like and i only want the values from row 3 and down.

        if sampleid!="000":
            print(sampleid)
            print ("Isotope \t", "A (Bq/kg) +/- 1 sd \t MDA (Bq/kg)")
            #sampleid = workbook.add_worksheet()
            
                    
                
            if create_an_excel_file_with_one_table:
                worksheet = workbook.add_worksheet(sampleid)
                worksheet.set_column('A:AA', 25)



        
       

            for i in range(len(activity)):
                if isotopes[i]=="Pb-210" :
                        worksheet.write(row+2, 0, isotopes[i])
                        worksheet.write(row+2, 1,str(np.round(activity_pbc[i]/mass,8)))
                        worksheet.write(row+2, 2, '+/-')
                        worksheet.write(row+2, 3,str(np.round(sigma_activity_pbc[i]/mass,8)))
                        worksheet.write(row+2, 4,str(MDA[i]/mass))     
      
 
                worksheet.write("B1", sampleid, bold)
                worksheet.write("A1", "Sampleid:", bold)
                worksheet.write("A2", "Isotope", bold)
                worksheet.write("B2","Activity (Bq/kg)", bold)
                worksheet.write("C2",'+/-', bold)
                worksheet.write("D2",'1 sd', bold)
                worksheet.write("E2","MDA (Bq/kg)", bold)
                worksheet.write(row,3,'Detector:', bold)
                worksheet.write(row,4,detector)
                worksheet.set_default_row(hide_unused_rows=True)
                                
                #worksheet.write(i,0, "SampleID")
    #worksheet.write(i,20, sampleid)         #worksheet.write(i,1, sampleid)
    workbook.close()  

正如我在评论中所说,无需进入特定代码,您可以使用 pandas 读取所有 sheets,随心所欲地操作它们 - 在这种情况下取​​第 3 行,然后继续, 并连接写回单个 sheet 文件:

df_list = pd.read_excel(<many_sheet_file>, sheet_name=None, header=None).values())
df_new_list = []
for df in df_list:
    df_new_list.append(df.iloc[2:])
df_all = pd.concat(df_new_list)
all_df.to_excel(<single_sheet_file>)