2015-10-20 184 views
0

当使用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 =阿尔及利亚),但吨将文件分发给用户时,他错误消息是不可取的。这也仅仅是个例子,我得到了同样的问题,如果我尝试类似:

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' 

回答

1

我不能说有关文件的东西没有看到,但随时提交一个错误报告,包含一个创建它的示例文件和脚本。如果您正在编辑现有文件,则错误几乎肯定与未由openpyxl保存的内容相关。

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

+0

叶认识到我有一种方式旧版本和更新到最新,现在工作!谢谢。 –