BeardedOne85
BeardedOne85

Reputation: 55

Wrapping text with =HYPERLINK and XlsxWriter

I am using Excel's =HYPERLINK function to write hyperlinks in XlsxWriter. I am not using write_url() because my tab names begin with numbers and I also need to show a "friendly" name instead of the hyperlink. (Showing a friendly name does not seem to work with write_url()'s 'internal:...' keyword.) The hyperlinking is working just fine but for some reason Excel is not wrapping the hyperlink even though I have set 'text_wrap' to True. As you can see in the top picture Excel shows that Wrap Text is set (it is grayed out) but the text is not wrapped. If I click Wrap Text to deselect it, and then click it again to re-select it, it does wrap the text, as seen in the bottom picture.

I am using xlsxwriter 1.0.2 and Excel 2016.

import xlsxwriter

wb = xlsxwriter.Workbook('Wrap.xlsx')

ws = wb.add_worksheet('Test1')

wrap = wb.add_format({'text_wrap': True})

ws.write('A1', '=HYPERLINK("#Sheet1!A1", "A really long name here that does not wrap")', wrap)
ws.write('B1', 'Bye')

wb.add_worksheet('Sheet1')
wb.close()

Link not wrapped

Link wrapped

Upvotes: 2

Views: 2174

Answers (2)

jmcnamara
jmcnamara

Reputation: 41644

Showing a friendly name does not seem to work with write_url()'s 'internal:...' keyword.

It does:

import xlsxwriter

wb = xlsxwriter.Workbook('Wrap.xlsx')
ws = wb.add_worksheet('Test1')

wrap = wb.add_format({'text_wrap': True})

ws.write_url('A1', 'internal:Sheet1!A1', wrap,
             "A really long name here that does not wrap")
ws.write('B1', 'Bye')

wb.add_worksheet('Sheet1')
wb.close()

Output:

enter image description here

Upvotes: 3

patrickjlong1
patrickjlong1

Reputation: 3823

You can use the worksheet.write_url() method and then use worksheet.write() to add formatting and text while preserving the hyperlink.

The code below produces the wrapped text.

import xlsxwriter

wb = xlsxwriter.Workbook('Wrap.xlsx')

ws = wb.add_worksheet('Test1')

wrap = wb.add_format({'text_wrap': True})

ws.write_url('A1',  'internal:Sheet1!A1')
ws.write('A1',  "A really long name here that does not wrap", wrap)
ws.write('B1', 'Bye')

wb.add_worksheet('Sheet1')
wb.close()

Expected Output:

Expected Output

Upvotes: 1

Related Questions