在openpyxl中获取格式数据

时间:2013-07-02 15:00:10

标签: python openpyxl

我无法使用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')

0 个答案:

没有答案
相关问题