繁体   English   中英

使用 python 将 Excel 工作表导入到一个 excel 文件

[英]Import Excel sheets to one excel file using python

我已经尝试了多种方法将数据循环放入带有 xlsxwriter、pandas 等的 excel 工作表中,但没有任何方法能达到我想要的效果。 所以我获得了很多数据。 现在我为每个新的“sampleid”制作一个新的 excel 表。 然后,我想只使用数据(而不是标题 bq/kg 等)获取每一行,并放入一个主 excel 文件,其中一张表用于所有“sampleid”。 有没有办法重写这段代码来循环它,以便将新的“sampleid”放在同一个工作表中,但只是低一行? 编辑:我上传了工作表的样子图片这是所有工作表的样子,我只想要第 3 行及以下的值。

        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 读取所有工作表,随意操作它们 - 在这种情况下,取第 3 行并继续,然后 concat 写回单个工作表文件:

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>) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM