简体   繁体   English

For循环使用XlsxWriter和Python Excel

[英]For loop using XlsxWriter with Python Excel

I'm trying to make a loop where new values are put into a new row in the same excel sheet.我正在尝试创建一个循环,将新值放入同一张 excel 工作表中的新行中。 I've succefully made a loop where for every new ''sampleid'' it made a new sheet but now I want for every new ''sampleid'' to do a new row with the values.我已经成功地创建了一个循环,它为每个新的“sampleid”创建了一个新的工作表,但现在我希望每个新的“sampleid”都用这些值做一个新的行。 This is the code im currently using, how can I modify it for the results I want?这是我目前使用的代码,如何修改它以获得我想要的结果?

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

I don't believe adding a new row with xlsxwriter is possible.我不认为可以使用xlsxwriter添加新行。 xlsxwriter is merely a module for writing files in any XLSX file format. xlsxwriter只是一个用于以任何XLSX文件格式编写文件的模块。 You can see here for more on what's offered.您可以在此处查看有关所提供内容的更多信息。

I would recommend using openpyxl for this as adding rows with that is much easier, see here for a comprehensive answer on how that's done - Insert row into Excel spreadsheet using openpyxl in Python我建议为此使用openpyxl ,因为用它添加行要容易得多,请参阅此处以获得有关如何完成的全面答案 - Insert row into Excel spreadsheet using openpyxl in Python

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

相关问题 使用 xlsxwriter 将 python 循环结果导出到 Excel 中的相应列 - Export python loop results to corresponding columns in Excel using xlsxwriter Python使用XlsxWriter将多个矩阵写入excel - Python write multiple matrix into excel using XlsxWriter 在 Python 中使用 Xlsxwriter 合并 Excel 单元格 - Merging Excel cells using Xlsxwriter in Python 使用dataframe和xlsxwriter内部的for循环将整个Beautifulsoup数组保存到excel中 - Saving whole Beautifulsoup array into excel using dataframe and xlsxwriter inside for loop Python - 使用 xlsxwriter 将数组值循环到 excel 文件中 - Python - Looping in array value into excel file using xlsxwriter Python:使用xlsxwriter从txt逐行导入excel - Python: Import from txt into excel line by line using xlsxwriter 如何在python中使用xlsxwriter冻结excel文件中的行和列 - how to freeze a row and a columns in an excel file using xlsxwriter in python 使用Python的xlsxwriter在Excel中将字符串条件格式设置为“等于” - String conditional formatting “equal to” in Excel using Python's xlsxwriter 如何在python中使用xlsxwriter从SQLITE导入数据到excel - how to import data from SQLITE to excel using xlsxwriter in python 使用 Python 和 XLSXWriter 将格式应用于多个 Excel 工作表 - Applying formatting to multiple Excel sheets using Python and XLSXWriter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM