1

I have a list with some values, eg. values = ['1', '2', '3']. I try to put them into one cell, but every value in new line, so it should looks like:
1
2
3
in one cell. I'm trying to do it like this:
sheet.write(1, 11, '\n'.join(values), style2)
but it seems not to work, because items are added in one line, so it looks like:
123

GohanP
  • 399
  • 2
  • 6
  • 18

3 Answers3

3

It should work using a \n newline in the string if you also specify text_wrap in the cell format (see the docs):

import xlsxwriter

workbook = xlsxwriter.Workbook('wrap.xlsx')
worksheet = workbook.add_worksheet()
wrap_format = workbook.add_format({'text_wrap': True})

values = ['1', '2', '3']

# Set an explicit row height for the wrapped text, if required.
#worksheet.set_row(1, 45)

worksheet.write(1, 1, '\n'.join(values), wrap_format)

workbook.close()

Output:

enter image description here

Note, when you wrap text in cells like this Excel normally adjusts the row height automatically to compensate unless you have already manually set the row height. I've put a commented out line in the example to show how to set the row height explicitly.

P.S. \r isn't required.

jmcnamara
  • 38,196
  • 6
  • 90
  • 108
0

Probably because of the EOF character, which can be expected to be \r, or \r\f, or \r\n:

sheet.write(1, 11, '\r\n'.join(values), style2)

It depends of the system, and the tool you use to visualize the file.

aluriak
  • 5,559
  • 2
  • 26
  • 39
  • It also doesn't work. With `'\r'` nothing changes and with `'\r\f'` some strange characters occures. I have win7 and codeing with pydev – GohanP Apr 04 '16 at 12:10
  • 1
    According to [this](http://stackoverflow.com/questions/2536545/how-to-write-unix-end-of-line-characters-in-windows-using-python), `\r\n` is a possible solution. – aluriak Apr 04 '16 at 12:14
  • Still everything in one line. I open a file in excel – GohanP Apr 04 '16 at 12:18
  • Ok, I checked it again, `'\r\n'` is working but only after double click on a cell, just realized that. It's also ok, so thanks for advice. – GohanP Apr 04 '16 at 12:33
0

According to this basic xlsxwriter tutorial, you have to provide the coordinates of each value, not an iterable containing the different values.

So, instead of

sheet.write(1, 11, '\n'.join(values), style2)

You should consider something like:

for offset, value in enumerate(values):
    sheet.write(1 + offset, 11, value, style2)
aluriak
  • 5,559
  • 2
  • 26
  • 39
  • Is there any reason you posted a second answer instead of integrating this to your previous one? It seems to follow up from the other one. Or if the other one was completely wrong, you could have changed it altogether to this. – Reti43 Apr 04 '16 at 12:29
  • That's not really the same type of answer. The previous was about dirty insertion of EOL character. This one is about using properly the package. Should i merge them ? – aluriak Apr 04 '16 at 12:30
  • It's not exactly what I need, because I want it in one cell, in one excel row but n new line in that cell, so ` 1 + offset ` is not the sollution. – GohanP Apr 04 '16 at 12:37