1

I write a testing Python program, as follow:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet("a")
worksheet.write(0,0, "b\r\nc\r\nd")
workbook.close()

It is expected there will be a cell filled with 3 lines. But when I open it, the three letters are in one line, until I double click the cell like this:

enter image description here

When I download a sheet from doc.google.com, the multiline cells will shrink into one line. After double-click, it will expands.

So, is there any way I can batch expand the cells rather than double click on each one?

Justyna MK
  • 3,523
  • 3
  • 11
  • 25
Jason Pan
  • 702
  • 7
  • 21
  • 1
    probably `set_text_wrap()`, and usually only `\n` is used for new line character in excel cells – Slai Dec 31 '19 at 09:09
  • @Slai Thanks a lot for your response. IT DOES WORK. But I may want find a trick in Excel so that I can use it also after downloading files from Google doc. It's why I only add an excel flag. I'll accept this if I found no Excel tricks in several days. – Jason Pan Dec 31 '19 at 09:41
  • seems like there is no way to apply formatting to existing file https://stackoverflow.com/a/22353696/1383168, so you might need different module for that – Slai Dec 31 '19 at 10:21
  • 1
    Before you double click, does the cell have Word Wrap on? Perhaps it's off, but by double clicking, it turns it on...therefore you could just apply word-wrapping to all the necessary cells? – BruceWayne Dec 31 '19 at 14:59
  • @BruceWayne Yes. Thanks. You have helped me get the answer. – Jason Pan Jan 02 '20 at 07:13

1 Answers1

0

The following steps work:

  1. Select all
  2. Click the Wrap Text Button(Home - Alignment - Wrap Text enter image description here)
  3. Adapt the width of columns and height of rows if necessary.

enter image description here

Thanks to @BruceWayne's question in comment.

Thanks for @Slai's Answer. In a Python script, set_text_wrap() can be used to make same influnce with the action above. The full code:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet("a")
cell_format = workbook.add_format()
cell_format.set_text_wrap()
worksheet.write(0,0, "b\nc\nd", cell_format)
workbook.close()
Jason Pan
  • 702
  • 7
  • 21