简体   繁体   中英

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. 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. 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 is merely a module for writing files in any XLSX file format. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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