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:

Using xlsxwriter, how do I add a newline character into a cell
Row height does not work when wrapping text
merge_range and text_wrap
excel - text_wrap format gets ignored using worksheet formatting - Stack Overflow
I'm trying to add a newline character in a cell using xlsxwriter. Can somebody please give me some ideas?
worksheet.write_string('A1', "New\nLine") gives me "New\nLine" within the cell A1. I want it to look like:
New
Line
Thank you!
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?
You can use pandas with the xlsxwriter engine (the default).
You need to create a format object by calling the workbook.add_format() method as outlined in the xlsxwriter docs (link here).
Once you've used pandas.DataFrame.to_excel(), you can add the format using worksheet.set_column(). An example of this can be found in the xlsxwriter docs (link here).
I've provided a fully reproducible example below with the expected output.
import pandas as pd
df = pd.DataFrame({'Ticket': ['a','b','c','d'],
'Category': [2,1,4,3]})
writer = pd.ExcelWriter('Trial Version.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook=writer.book
worksheet = writer.sheets['Sheet1']
format = workbook.add_format({'text_wrap': True})
# Setting the format but not setting the column width.
worksheet.set_column('A:B', None, format)
writer.save()
Expected Output:

Use python xlsxwriter
The question answered previously can help you better.
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:

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.
