Search code examples
pythonxlwt

Writing data to excel iteratively


I have 2 lists and below is what i wanna do: b = ['ibm cloud','dell cloud'] a =['wen','see','there']

I want to create 2 different worksheets with the list items in b. and then write to the worksheets the list items from a in the rows. And i am unable to do so.

import xlwt base = xlwt.Workbook() for a in a: sheet = base.add_sheet(a) for b in b: sheet.write(row,col,b)

Please help


Solution

  • You said create 2 different worksheets with items in b, but you use items in a in your code. Why? And try not to use same name when using for loop. With this code, you can get a xls with two sheets, named 'company1' and 'company2'. In each sheet, from row 0 to row 2, there will be value defined in a. Last but not least, don't forget to save. Try this:

    #!/usr/bin/python
    #-*- coding:utf-8 -*-
    
    import xlwt
    
    base = xlwt.Workbook()
    
    b = ['company1','company2']
    a = ['a', 'b', 'c']
    
    for name in b:
        n = 0
        s = base.add_sheet(name);
        for v in a:
            s.write(n, 0, v)
            n += 1
    
    base.save('C:\\test.xls')