[英]How to put rich text in a merged cell using xlwt
Following up on an earlier question about a subset of the functionality I need, I'm trying to put rich text in a merged cell of an Excel worksheet created using the xlwt package. 在回答了有关我需要的功能子集的先前问题之后,我试图将富文本放入使用xlwt包创建的Excel工作表的合并单元格中。 I know about sheet.write_merge()
and (with the help of that earlier question) I found sheet.write_rich_text()
but there doesn't appear to be a way to do both. 我知道sheet.write_merge()
和(在先前的问题的帮助下)我找到了sheet.write_rich_text()
但似乎没有办法做到这两种。 Excel supports it, so at least in theory it should be possible for xlwt to do it. Excel支持它,因此至少从理论上讲,xlwt应该可以做到这一点。 Is the functionality there and I just haven't found it? 有功能吗,我还没有找到?
Thanks! 谢谢!
I still haven't found a sheet.write_rich_text_merge()
method in the API which does what I need to do here, but I did find by experimenting an approach which appears to work. 我仍然没有在API中找到一个sheet.write_rich_text_merge()
方法来执行我在这里需要做的事情,但是我通过尝试一种看起来可行的方法找到了。 What you have to do is 你要做的是
For example: 例如:
import xlwt
book = xlwt.Workbook()
style = xlwt.easyxf("align: wrap true, horiz left, vert top")
comment_font = xlwt.easyfont("italic true, color_index green")
sheet = book.add_sheet("Merged Rich Text", cell_overwrite_ok=True)
sheet.col(0).width = sheet.col(1).width = 7500
drug_name = "atezolizumab"
comment = "For NCT01375842; No info available at this time"
segments = (drug_name, "\n", ("[%s]" % comment, comment_font))
other_names = ("anti-PD-L1 monoclonal antibody", "MPDL3280A", "RG7446")
sheet.write_merge(0, len(other_names) - 1, 0, 0, "", style)
sheet.row(0).set_cell_rich_text(0, segments, style)
for i, name in enumerate(other_names):
sheet.write(i, 1, name, style)
fp = open("repo.xls", "wb")
book.save(fp)
fp.close()
It would be preferable to have an API function which handled the merge and the rich text formatting with a single call, without the need to suppress the check for overwriting cells, but if you're always careful to test your code by temporarily commenting out the set_cell_rich_text()
call and removing the flag to allow cell overwriting, you'll still be able to catch bugs elsewhere in which cells are being overwritten by mistake. 最好有一个API函数通过一个调用来处理合并和RTF格式,而无需抑制对覆盖单元格的检查,但是如果您始终小心地通过临时注释掉代码来测试代码, set_cell_rich_text()
调用并删除该标志以允许单元覆盖,您仍然可以捕获其他地方错误覆盖单元的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.