簡體   English   中英

For循環使用XlsxWriter和Python Excel

[英]For loop using XlsxWriter with Python Excel

我正在嘗試創建一個循環,將新值放入同一張 excel 工作表中的新行中。 我已經成功地創建了一個循環,它為每個新的“sampleid”創建了一個新的工作表,但現在我希望每個新的“sampleid”都用這些值做一個新的行。 這是我目前使用的代碼,如何修改它以獲得我想要的結果?

        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', 30)

            for i in range(len(activity)):
                if isotopes[i]=="Pb-210" :
                        worksheet.write(i+2, 0, isotopes[i])
                        worksheet.write(i+2, 1,str(np.round(activity_pbc[i]/mass,8)))
                        worksheet.write(i+2, 2, '+/-')
                        worksheet.write(i+2, 3,str(np.round(sigma_activity_pbc[i]/mass,8)))
                        worksheet.write(i+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)

    workbook.close()    

我不認為可以使用xlsxwriter添加新行。 xlsxwriter只是一個用於以任何XLSX文件格式編寫文件的模塊。 您可以在此處查看有關所提供內容的更多信息。

我建議為此使用openpyxl ,因為用它添加行要容易得多,請參閱此處以獲得有關如何完成的全面答案 - Insert row into Excel spreadsheet using openpyxl in Python

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM