使用 XlsxWriter 和 Python Excel 的循环

For loop using XlsxWriter with Python Excel

我正在尝试创建一个循环,将新值放入同一个 excel sheet 的新行中。我已经成功地创建了一个循环,其中每个新的“'sampleid'”都创建了一个新的 sheet,但现在我希望每个新的“'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 文件格式写入文件的模块。您可以查看 here 以了解有关所提供内容的更多信息。

我建议为此使用 openpyxl,因为用它添加行要容易得多,请参阅此处以获得有关如何完成的全面答案 - Insert row into Excel spreadsheet using openpyxl in Python