Links in Excel have cell formatting (generally blue text and an underline) like any other formatted text.

If no other formatting is specified for links in XlsxWriter the module adds a default (blue underline) format, like Excel does when you enter a url. This is explained in the write_url() docs.

However, if the user specifies a format for a cell with a link (like the text_wrap format in your example) then it overrides the default format.

So if you want blue underline plus text wrap format for urls you will have to specify it directly:

import xlsxwriter

workbook = xlsxwriter.Workbook('my_export.xlsx')
worksheet = workbook.add_worksheet(name='export_object1')

link_format = workbook.add_format({'color': 'blue', 
                                   'underline': True, 
                                   'text_wrap': True})

text_format = workbook.add_format({'text_wrap': True})

worksheet.write('A1', 'Its\na bum\nwrap',           text_format)
worksheet.write('B1', 'http://stackoverflow.com/',  link_format)

workbook.close()

Output:

Answer from jmcnamara on Stack Overflow
🌐
XlsxWriter
xlsxwriter.readthedocs.io β€Ί format.html
The Format Class - XlsxWriter - Read the Docs
The height of the cell will be adjusted to accommodate the wrapped text. To specify where the text wraps use the set_text_wrap() method.
Discussions

Using xlsxwriter, how do I add a newline character into a cell
I'm not familiar with this module but have you tried \r\n? This is the windows new line symbol. More on reddit.com
🌐 r/learnpython
5
2
June 1, 2014
Row height does not work when wrapping text
Reporting Bug Hi, I am using Python version 3.8.10 and XlsxWriter 1.4.4 and Excel version 2016. Here is some code that demonstrates the problem: import xlsxwriter workbook = xlsxwriter.Workbook(... More on github.com
🌐 github.com
4
July 21, 2021
merge_range and text_wrap
I tried to merge some cells and ... with text_wrap set to True. Unfortunately the row height is not auto expanded. Setting the row height manually is not very precise as it's very difficult to accurately calculate how many rows a string needs... from xlsxwriter.workbook import ... More on github.com
🌐 github.com
3
May 21, 2015
excel - text_wrap format gets ignored using worksheet formatting - Stack Overflow
Is this referring to xlsxwriter? Can you show what you trying to add to the cell and show an image of how it looks? The default behaviour of 'text_wrap':True is to wrap the text and it works perfectly fine. ... I added an answer that may help. ... Pandas writes the dataframe header with a default cell format. Since it is a cell format it cannot be overridden using set_row... More on stackoverflow.com
🌐 stackoverflow.com
March 15, 2019
🌐
GitHub
github.com β€Ί jmcnamara β€Ί XlsxWriter β€Ί issues β€Ί 80
text_wrap format is ignored?.. Β· Issue #80 Β· jmcnamara/XlsxWriter
December 18, 2013 - out = StringIO.StringIO() workbook = xlsxwriter.workbook.Workbook(out) worksheet = workbook.add_worksheet() header_format = workbook.add_format({'text_wrap': 1, 'valign': 'top'}) worksheet.write('A1', u'Кол-Π²ΠΎ РСкламоноситСлСй', header_format) # self._write_header(workbook, worksheet) # self._write_body(*args) # self._write_footer(worksheet) workbook.close() out.seek(0) return out ... As far as I undertood the docs, the row height should have been auto-adjusted, or am I missing something?
Author: jmcnamara
🌐
GitHub
gist.github.com β€Ί braindevices β€Ί f7a5f27bdb8f7513620a1ee48f630b03
how to autowrap the text in excel when export from pandas dataframe styler Β· GitHub
import pandas as pd data = pd.DataFrame({"A":list(range(10,12)), "B":["abcdefgafafadf", "afbafafafafadfadfadf"]}) with pd.ExcelWriter("/tmp/test.xlsx") as writer: data.style.apply(lambda x: ['background-color: red']*2 if x['A']==11 else [None]*2, axis=1).to_excel(writer, index=False) writer.sheets["Sheet1"].write(3, 1, "Some long text to wrap in a cell") wrap_format = writer.book.add_format({'text_wrap': True}) writer.sheets["Sheet1"].set_column('A:B', 5, wrap_format) After opening the file in excel. We can see the first 2 rows does not have the text_wrap feature. But the rest of the rows has it. According to XlsxWriter authors, the reason is that In XlsxWriter generated files a cell format overrides a column format.
🌐
GitHub
github.com β€Ί jmcnamara β€Ί XlsxWriter β€Ί issues β€Ί 817
Row height does not work when wrapping text Β· Issue #817 Β· jmcnamara/XlsxWriter
July 21, 2021 - import xlsxwriter workbook = xlsxwriter.Workbook('test.xlsx') worksheet = workbook.add_worksheet('Test') worksheet.set_row(0, 15) worksheet.write(0, 0, 'Π’Π΅Π»ΠΎ запроса с названиями Ρ‚ΠΎΠ²Π°Ρ€ΠΎΠ² ΠΊ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°ΠΉΡ‚ΠΈ Π²Π΅Ρ€Π½ΡƒΡŽ модСль', workbook.add_format({'text_wrap': True})) workbook.close() Result: But I need to set the row height to 15 Β·
Author: jmcnamara
🌐
Libxlsxwriter
libxlsxwriter.github.io β€Ί working_with_formats.html
libxlsxwriter: Working with Formats
lxw_error worksheet_set_row(lxw_worksheet *worksheet, lxw_row_t row, double height, lxw_format *format)
🌐
GitHub
github.com β€Ί jmcnamara β€Ί XlsxWriter β€Ί issues β€Ί 259
merge_range and text_wrap Β· Issue #259 Β· jmcnamara/XlsxWriter
May 21, 2015 - I tried to merge some cells and to write in them using merge_range, passing a style with text_wrap set to True. Unfortunately the row height is not auto expanded. Setting the row height manually is not very precise as it's very difficult to accurately calculate how many rows a string needs... from xlsxwriter.workbook import Workbook workbook = Workbook('wrap_text2.xlsx') worksheet = workbook.add_worksheet() # Widen the first column to make the text clearer.
Author: jmcnamara
Find elsewhere
🌐
XlsxWriter
xlsxwriter.readthedocs.io β€Ί example_comments2.html
Example: Adding Cell Comments to Worksheets (Advanced) β€” XlsxWriter
# # Set up some formatting. worksheet8.set_column("C:C", 25) worksheet8.set_row(2, 80) worksheet8.show_comments() cell_text = ( "The height of this row has been adjusted explicitly using " "set_row(). The size of the comment box is adjusted " "accordingly by XlsxWriter." ) comment = "Hello." worksheet8.write("C3", cell_text, text_wrap) worksheet8.write_comment("C3", comment) cell_text = ( "The height of this row has been adjusted by Excel due to the " "text wrap property being set.
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί xlsxwriter, panda formatting my headers
r/learnpython on Reddit: xlsxwriter, panda formatting my headers
December 15, 2016 -

Good morning, I've been having some fun lately with moving some of my work to Python automation. Previously I was running SQL manually, pasting to Excel then manually moving files and formatting them. I have replaced almost all of that with Python. I'm struggling with a little snag in the formatting.

I can dropping my data into a panda dataframe which is going to Excel. I'm adjusting my column sized but want to apply a format to the top row. I'm trying to use set_row() but my format doesn't seem to be sticking.

I'm trying this:

text_wrap = workbook.add_format({'text_wrap': 1, 'valign': 'top'})
worksheet.set_column('A:A',8)
worksheet.set_column('B:B',12)
...
worksheet.set_column('Z:Z',8)
worksheet.set_row(0,45,text_wrap )

but it doesn't seem to be working. I've also tried:

text_wrap2 = workbook.add_format()
text_wrap2.set_text_wrap()

When I look for samples of applying text_wrap to cells online they all seem to use write() which I don't think I can do with the dataframe approach I'm using. does anyone have any experience in how to apply the text_wrap attribute to a head row that is already written?

🌐
XlsxWriter
xlsxwriter.readthedocs.io β€Ί worksheet.html
The Worksheet Class β€” XlsxWriter
In Excel only the font properties of the format such as font name, style, size, underline, color and effects are applied to the string fragments in a rich string. Other features such as border, background, text wrap and alignment must be applied to the cell.
🌐
The Document Foundation
bugs.documentfoundation.org β€Ί show_bug.cgi
123026 – LibreOffice ignore xlsxwriter 'text_wrap' formatting option, seems optimal height for row is not being set to hold cells with wrapped text
January 28, 2019 - Bugzilla – Bug 123026 LibreOffice ignore xlsxwriter 'text_wrap' formatting option, seems optimal height for row is not being set to hold cells with wrapped text Last modified: 2025-05-15 19:34:18 UTC
🌐
TutorialsPoint
tutorialspoint.com β€Ί python_xlsxwriter β€Ί python_xlsxwriter_quick_guide.htm
Python XlsxWriter - Quick Guide
To do this using XlsxWriter, we need to use the level property of the set_row() method.
Top answer
1 of 6
7

As noted in the XlsxWriter docs, DataFrame formatting cannot be overwritten using set_row(). The easiest way to change the formatting of a DataFrame header is to overwrite the header with individual cell entries, overwriting the value and the format of each cell:

import pandas as pd
import xlsxwriter

df = pd.DataFrame({'this is a very long header that should wrap': ['a2', 'a3', 'a4'],
                   'header2': [1, 2, 3],
                   'header3': [4, 5, 6]})

writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False)

workbook  = writer.book
worksheet = writer.sheets['Sheet1']
header_format = workbook.add_format({'bold': True, 'text_wrap': True, 'valign': 'top'})

# Overwrite both the value and the format of each header cell
for col_num, value in enumerate(df.columns.values):
    worksheet.write(0, col_num + 1, value, header_format)

workbook.close()

The code above will produce the following:

2 of 6
1

I have spent hours troubleshooting this same problem. I have tried the following:

header_format = workbook.add_format({'bold': True, 'text_wrap': True, 'valign': 'top'})

And setting it through its own statement:

header_format.set_text_wrap()

And adding a boolean to the statement:

header_format.set_text_wrap(True)

I thought maybe it was because I was using conditional formatting:

worksheet.conditional_format(0, 0, 0, 21, {'type': 'no_blanks', 'format': header_format})

So I tried it without:

worksheet.set_row(0, 45, testing_format)

I thought that maybe because it was coming before the statement setting column widths below it that perhaps it needed to be at the very end (even though no other format settings exhibit this behavior). That didn't work.

I tried to use one formatting with only text_wrap set and no other formatting and used in no other places. That didn't work.

I tried it with it being the only formatting set for the entire worksheet. That didn't work

I tried updating XlsxWririter to 1.3.7. That didn't work.

At this point I can say with 100% confidence that text wrapping does not work in XlsxWriter 1.3.7 with Python 3.7.4, and Excel for Microsoft 365 MSO 64-bit.

🌐
Hexdocs
xlsx-writer.hexdocs.pm β€Ί formatting.html
Advanced Formatting Guide β€” xlsx_writer v0.10.0
sheet = XlsxWriter.new_sheet("Wrapped Text") # Basic text wrapping |> XlsxWriter.write(0, 0, "This is a long text that will wrap within the cell", format: [:text_wrap]) # Combine with other formatting |> XlsxWriter.write(1, 0, "Bold wrapped text with background", format: [:text_wrap, :bold, {:bg_color, "#FFFF00"}]) # Set column width for better wrapping |> XlsxWriter.set_column_width(0, 20) {:ok, content} = XlsxWriter.generate([sheet]) File.write!("wrapped.xlsx", content)
🌐
Google Groups
groups.google.com β€Ί g β€Ί spreadsheet-writeexcel β€Ί c β€Ί YF4dd6RIlx0
Excel::Writer::XLSX - Text wrap is not working for text written with write_rich_string()
Hi, I want to write multi-line, rich formatted text to a merged cell but text wrap does not seem to work correctly when using write_rich_string(). For example, I ran some tests by modifying the cell allignments portion of the formats.pl example code provided with the download, the only changes I made were as follows: I set the format variable as follows: my $format06 = $workbook->add_format(); $format06->set_text_wrap(); then merge the range: $worksheet->merge_range("F1:F2", '', $format06 ); now, writing cell contents using write() works correctly (although it's not possible to format lines individually).
🌐
XlsxWriter
xlsxwriter.readthedocs.io β€Ί working_with_textboxes.html
Working with Textboxes β€” XlsxWriter
The width and height are in pixels. The default textbox size is 192 x 120 pixels (or equivalent to 3 default columns x 6 default rows). The size of the textbox can be modified by setting the width and height or by setting the x_scale and y_scale: