Python 在openpyxl中获取格式化数据

Python 在openpyxl中获取格式化数据,python,openpyxl,Python,Openpyxl,我在使用openpyxl从excel工作表中提取样式时遇到问题,在下面的示例中,我正在创建一个电子表格,我可以看到格式是正确的,但我不知道如何取回该数据 在我的实际用例中,我只是在读取一个文件——我不是创建它的人,所以我希望能够以编程方式检索格式 from openpyxl import Workbook from openpyxl.reader.excel import load_workbook from openpyxl.style import Color, Fill #this is

我在使用openpyxl从excel工作表中提取样式时遇到问题,在下面的示例中,我正在创建一个电子表格,我可以看到格式是正确的,但我不知道如何取回该数据

在我的实际用例中,我只是在读取一个文件——我不是创建它的人,所以我希望能够以编程方式检索格式

from openpyxl import Workbook
from openpyxl.reader.excel import load_workbook
from openpyxl.style import Color, Fill
#this is all setup
wb = Workbook()
dest_filename = 'c:\\temp\\test.xlsx'

ws = wb.worksheets[0]

ws.title = 'test'

ws.cell('A1').value = 'foo'
ws.cell('A1').style.font.bold = True

ws.cell('B1').value = 'bar'
ws.cell('B1').style.fill.fill_type = Fill.FILL_SOLID
ws.cell('B1').style.fill.start_color.index = Color.DARKYELLOW

wb.save(filename = dest_filename )
#setup complete    

book = load_workbook( filename = dest_filename )

sheet = book.get_sheet_by_name('test')

#value work properly
print sheet.cell('A1').value #returns foo
print sheet.cell('B1').value #return bar

#formatting does not - THIS IS THE PROBLEM CODE
print sheet.cell('A1').style.font.bold #returns False
print sheet.cell('B1').style.fill.fill_type #returns none
print sheet.cell('B1').style.fill.start_color.index #returns FFFFFFFF

print sheet.cell('B1').has_style #returns true
#but these 2 return the same values! even thought C1 was never set and should be different
print sheet.get_style('A1')
print sheet.get_style('C1')

你能把这段代码删减到只会给你带来麻烦的部分吗?它实际上已经非常小了——如果你安装了openpyxl,你可以复制粘贴,它应该可以工作。如果你只是想看到一些具体的问题,那就是最后6行——所有的打印语句(has_样式除外)都返回了“错误”的值。你使用的是什么
openpyxl
version?我已经在本地试用了你的代码-它可以正确读取样式。我以前使用的是1.5.8,现在使用的是1.6.2,它可以正常工作。谢谢你的帮助!如果你把这个评论作为一个答案,我很乐意把它标记为解决方案。