如何在Python 2.7中分配styleObj后使用工作表['A'] .style / styleObj指定样式?

问题描述 投票:-3回答:1

Problem with sheet['A'].style/styleObj in Python 2.7

What might be wrong?

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!'
python python-2.7 openpyxl
1个回答
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!'
© www.soinside.com 2019 - 2024. All rights reserved.