DEV Community

Vijay Lakhujani
Vijay Lakhujani

Posted on

How to write in specific number of columns in excel file using xlwt?

I have a list say

po = ['Mon', '6421', 'Tue', '6412', 'Wed', '12416', 'Thu', '23483', 'Fri', '8978', 'Sat', '7657', 'Sun', '6555']

and I wish to write this in an excel file based on user provided no. of sets, for e.g. say if user says "2" sets then this list in the excel look like

Mon 6421    Tue 6412
Wed 12416   Thu 23483
Fri 8978    Sat 7657
Sun 6555

So effectively, the data is populated in 4 excel columns.


Similarly, if the user say "3" sets, then the data look like

Mon 6421    Tue 6412    Wed 12416
Thu 23483   Fri 8978    Sat 7657
Sun 6555

So effectively, the data is populated in 6 excel columns.


I have tried something like this but I dont know what to write in place of ? and line#14. Can you please help?

1 import xlsxwriter
2
3 wb = xlsxwriter.Workbook('dem.xlsx')
4 ws = wb.add_worksheet("New Sheet")
5
6 po = ['Mon', '6421', 'Tue', '6412', 'Wed', '12416', 'Thu', '23483', 'Fri', '8978', 'Sat', '7657', 'Sun', '6555']
7 user_set = 4
8
9 num_row  = ((len(po)/user_set) / 2 ) + 1
10
11
12 for row in range(0,num_row):
13    for col in range(0,user_set):
14      ws.write_row(row, col, ? )
15
16 wb.close()

Top comments (0)