Search code examples
pythonarraysexport-to-excel

How to write Python Array into Excel Spread sheet


I am trying to write following array into Excel spreadsheet using Python:

array = [ [a1,a2,a3], [a4,a5,a6], [a7,a8,a9], [a10, a11, a12, a13, a14]]

At spreadsheet array should look:

a1  a4  a7  a10
a2  a5  a8  a11
a3  a6  a9  a12
            a13
            a14

Is anyone can show some Python code to do it? Thank you in advance,

Felix


Solution

  • Here is one way to do it using the XlsxWriter module:

    import xlsxwriter
    
    workbook = xlsxwriter.Workbook('arrays.xlsx')
    worksheet = workbook.add_worksheet()
    
    array = [['a1', 'a2', 'a3'],
             ['a4', 'a5', 'a6'],
             ['a7', 'a8', 'a9'],
             ['a10', 'a11', 'a12', 'a13', 'a14']]
    
    row = 0
    
    for col, data in enumerate(array):
        worksheet.write_column(row, col, data)
    
    workbook.close()
    

    Output:

    enter image description here