1

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

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

this is the code I am using right now:

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() 

This is the Output I am getting:

This is the output im getting

I want it to print it like this:

enter image description here

2 Answers 2

1

Using join() and set_text_wrap()

https://xlsxwriter.readthedocs.io/workbook.html#workbook-add-format https://xlsxwriter.readthedocs.io/format.html#set_text_wrap

import xlsxwriter

workbook = xlsxwriter.Workbook('arrays.xlsx')
worksheet = workbook.add_worksheet()

cell_format = workbook.add_format()
cell_format.set_text_wrap()
cell_format.set_align('vcenter')

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(row, col, '\n'.join(data), cell_format)

workbook.close() 
Sign up to request clarification or add additional context in comments.

2 Comments

thank you @User863 if u dont mind what can i do to make the list print from the beginning in the cell
remove the line set_align('vcenter')
0

I think you should use a "\n" to store them all in a single cell.

Try to put something like this:

array = ['\n'.join(i) for i in array]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.