openpyxl不可读的内容

当使用openpyxl,如果我更新像这样的单元格样式:

wb = Workbook() ws = wb.active a1 = ws['a1'] al.value = 'Hello World!' a1.style.font.name = 'Algerian' 

在Excel中打开文件给出了一个错误:

“Excel发现不可读的内容…”

日志文件:

 <?xml version="1.0" encoding="UTF-8" standalone="true"?> -<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> <logFileName>error107840_01.xml</logFileName> <summary>Errors were detected in file 'D:\Phocas\Phocas-Automation\test.xlsx'</summary> -<removedRecords summary="Following is a list of removed records:"> <removedRecord>Removed Records: Cell information from /xl/worksheets/sheet1.xml part</removedRecord> </removedRecords> </recoveryLog> 

单元格显示正确的样式(font.name = Algerian),但在将文件分发给用户时,错误消息是不可取的。 这也只是一个例子,我得到同样的问题,如果我尝试像这样:

 a1.style.font.bold = True 

除了直接更新类实例中的属性之外,还有其他方法可以更新单元格样式吗?

当我在https://openpyxl.readthedocs.org/en/latest/usage.html阅读文档时,它说使用:

 ft = Font(color=colors.RED) a1.font = ft 

但是这给了我一个属性错误:

 AttributeError: 'Cell' object has no attribute 'font' 

我无法看到任何关于该文件的内容,但随时可以提交一个带有创build它的示例文件和脚本的错误报告。 如果你正在编辑一个已经存在的文件,那么这个错误几乎肯定与openpyxl没有保存的东西有关。

但是, cell.font = Font(…)不起作用的事实表明您有相当旧的版本的库。 我build议你用pip install -U --pre更新到2.3-b2