Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python XlsxWriter text wrapping and links styling

I need help with python XlsxWriter. I need to add link styling for external file link columns. But Xlsxwriter doesn't recognize styling for links(its second column) (text: underline, text-color: blue) if I'm adding the text wrapping for another columns(in this example first column).

Here is my example:

# _*_ coding: utf-8
import xlsxwriter

wb = xlsxwriter.Workbook('/home/mtw/Downloads/my_export.xlsx')

format = wb.add_format()
format.set_text_wrap()

sheet = wb.add_worksheet(name='export_object1')
sheet.write_row('A1', [
    u'Its\na bum\nwrap',
    'external:resignation_letter.docx',
], format)
wb.close()

So I need to tell the XlsxWriter that he can recognize and text wrapping and styling for links.

Microsoft office: 2007.

xlsxwriter latest version.

Thx.

like image 766
Igor Komar Avatar asked Mar 02 '16 11:03

Igor Komar


1 Answers

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:

enter image description here

like image 167
jmcnamara Avatar answered Oct 18 '22 13:10

jmcnamara