python XlsxWriter text wrapping and links styling

倖福魔咒の 提交于 2019-12-10 17:54:52

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/35745451/python-xlsxwriter-text-wrapping-and-links-styling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!