在Python 2.7中分配了styleObj之后,如何使用sheet ['A']。style / styleObj分配样式?

时间:2019-04-11 04:41:21

标签: python python-2.7 openpyxl

Python 2.7中sheet ['A']。style / styleObj的问题

怎么了?

import openpyxl
from openpyxl.styles import Font, NamedStyle
# create new file
wb = openpyxl.Workbook()
# read active sheet
sheet = wb.get_active_sheet()
# give new name parameters 
italic24Font = Font(size=24, italic=True)
styleObj = NamedStyle(font=italic24Font)
sheet['A'].style/styleObj
sheet['A1'] = 'Hello world!'

1 个答案:

答案 0 :(得分:0)

感谢大家的帮助。 我找到了正确的代码版本:

import openpyxl
from openpyxl.styles import Font, NamedStyle
# create new file
wb = openpyxl.Workbook()
# read active sheet
sheet = wb['Sheet']

# give new name parameters 
italic24Font = NamedStyle(name="italic24Font")
italic24Font.font = Font(size=24, italic=True)
sheet['A1'].style = italic24Font
sheet['A1'] = 'Hello world!'