Python xlsxwriter 修复记录:来自 /xl/sharedStrings.xml 部分的字符串属性(字符串)

Python xlsxwriter Repaired Records: String properties from /xl/sharedStrings.xml part (Strings)

我正在使用 xlsxwriter 模块创建并写入 excel 文件。但是当我打开 excel 文件时,我得到这个弹出窗口:

We found a problem with some content in 'excel_sheet.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes. 如果我单击是,它显示 Repaired Records: String properties from /xl/sharedStrings.xml part (Strings) 然后我可以看到内容。

我发现这是因为我使用 write_rich_string 编写的单元格。

my_work_sheet.write_rich_string(row_no, col_no,format_1, "Some text in format 1", format_2, "Text in format 2", format_1, "Again in format 1")

如果我使用 write_string 编写它,则不会发生这种情况。 format_1format_2 设置了字体名称、颜色、大小和垂直对齐。

谁能指出这里出了什么问题?

一般来说,Excel 中的 "We found a problem with some content" error/warning 意味着 XML 元素或属性在 xlsx 文件的其中一个组件文件中格式不正确。同样,一般来说,这意味着 XlsxWriter 中存在错误,或者它未能以某种方式清理用户输入。

但是,在这种特殊情况下,没有足够的信息来确定问题所在。我把你的代码片段变成了一个测试程序,它按预期工作。

import xlsxwriter

workbook = xlsxwriter.Workbook('rich_strings.xlsx')
my_work_sheet = workbook.add_worksheet()

my_work_sheet.set_column('A:A', 50)

format_1 = workbook.add_format({'color': 'red'})
format_2 = workbook.add_format({'color': 'blue'})

my_work_sheet.write_rich_string(0, 0, 
                                format_1, "Some text in format 1 ", 
                                format_2, "Text in format 2 ", 
                                format_1, "Again in format 1")

workbook.close()

输出:

因此,您需要像上面一样创建一个独立的小程序并提交 a bug report to the XlsxWriter project

我试图重现(感谢@jmcnamara)这个问题,我可以找出问题所在。

在我对 write_rich_string 的命令中,有时它试图格式化空字符串。

my_work_sheet.write_rich_string(row_no, col_no,format_1, string_1, format_2, string_2, format_1, string_3)

我知道在某个时间点 string_1string_2string_3 中的一个的值变为 ''。现在我只在确保它们不是 ''.

后才使用 write_rich_string