+2 votes
in Programming Languages by (7.8k points)
I want to create an XLS/XLSX file using Python. Can you give me an example.

1 Answer

0 votes
by (15.1k points)

You can use xlsxwriter module of Python. Have a look at the following example.

import xlsxwriter
#from matplotlib import cm

def main():
    xlsfile = 'test.xlsx'
    uid = (['AB',11], ['BC',12], ['CD',13], ['DE',14], ['EF',15])
    workbook = xlsxwriter.Workbook(xlsfile)
    worksheet = workbook.add_worksheet()
    r = 0 #start row
    c = 0 #start column
    for name, id in (uid):
        worksheet.write(r, c, name)
        worksheet.write(r, c+1, id)
        r+=1
    workbook.close()

if __name__ == '__main__':
    main()

Output xlsx file has the following data.

AB11
BC12
CD13
DE14
EF15
...