How to use wrapText alignment style

5,342 views
Skip to first unread message

robertso...@gmail.com

unread,
Jun 6, 2016, 3:29:23 AM6/6/16
to openpyxl-users
Hi there,

I'm trying to use the `wrapText` alignment style as per this SO question, but I cannot get text wrapping to work:
from openpyxl import Workbook

workbook = Workbook()
worksheet = workbook.worksheets[0]
worksheet.title = "Sheet1"

worksheet.cell('A1').style.alignment.wrap_text = True
worksheet.cell('A1').value = "Line 1\nLine 2\nLine 3"

workbook.save('wrap_text1.xlsx')

Neither wrap_text or wrapText work


I originally posted this on the bitbucket issue list but was told this is the place for questions/bug report so am re-posting :-)
Apparently the SO question is old and after some minor changes this code will work. Anybody know what the minor changes might be?

Thanks!


Charlie Clark

unread,
Jun 6, 2016, 3:32:54 AM6/6/16
to openpyx...@googlegroups.com
Am .06.2016, 09:29 Uhr, schrieb <robertso...@gmail.com>:

> Hi there,
>
> I'm trying to use the `wrapText` alignment style as per this SO question
> <https://stackoverflow.com/questions/15370432/writing-multi-line-strings-into-cells-using-openpyxl>,
> but I cannot get text wrapping to work:
>
> from openpyxl import Workbook
> workbook = Workbook()worksheet = workbook.worksheets[0]worksheet.title =
> "Sheet1"
> worksheet.cell('A1').style.alignment.wrap_text =
> Trueworksheet.cell('A1').value = "Line 1\nLine 2\nLine 3"
> workbook.save('wrap_text1.xlsx')
>
> Neither wrap_text or wrapText work
>
> I originally posted this on the bitbucket issue list but was told this is
> the place for questions/bug report so am re-posting :-)
> Apparently the SO question is old and after some minor changes this code
> will work. Anybody know what the minor changes might be?
> https://bitbucket.org/openpyxl/openpyxl/issues/635/wraptext-alignment-style-not-working
>
The problem is that "style" objects have been deprecated. You can now work
directly with cell's alignment objects. However, as styling is immutable
because it's shared you cannot manipulate but must rebind or assign new
styling, eg.

ws['A1'].alignment = Alignment(wrapText=True)

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

Patrick Robertson

unread,
Jun 6, 2016, 3:37:57 AM6/6/16
to openpyx...@googlegroups.com
Great, the updated SO question helps a lot. For reference, here’s the code:

from openpyxl import Workbook
from openpyxl.styles import Alignment

wb = Workbook()
ws = ws.active
ws['A1'] = "Line 1\nLine 2\nLine 3"

ws['A1'].alignment = Alignment(wrapText=True)

wb.save("wrap.xlsx")

It would be great if the alignment and wrapText properties could be made read-only, or a warning/error emitted when the user tries to assign a value to this. That would help with backwards compatibility.

--
You received this message because you are subscribed to a topic in the Google Groups "openpyxl-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openpyxl-users/LdCzH3bQOog/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openpyxl-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Charlie Clark

unread,
Jun 6, 2016, 3:52:53 AM6/6/16
to openpyx...@googlegroups.com
Am .06.2016, 09:37 Uhr, schrieb Patrick Robertson
<robertso...@gmail.com>:

> It would be great if the alignment and wrapText properties could be made
> read-only, or a warning/error emitted when the user tries to assign a
> value to this. That would help with backwards compatibility.

Style objects are immutable. Try this:
ws['A1'].alignment.wrapText = True

We did try making style properties immutable but, as Python doesn't do
immutable attributes, this is a long of work and very slow. We had to
break backwards compatibility for this but did document the changes.
StackOverflow is a great resource but it is *unofficial* unlike the
documentation and the release notes.
Reply all
Reply to author
Forward
0 new messages