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!'
谢谢大家的帮助。我找到了正确的代码版本:
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!'