简体   繁体   中英

How to assign a style using sheet['A'].style/styleObj after styleObj has been assigned in Python 2.7?

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!'

Thanks everyone for your help. I found a correct version of the code:

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!'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM