Here is my code, please note: am currently learning Python:
import openpyxl
from openpyxl.styles import Font
wb = openpyxl.Workbook()
sheet = wb.get_sheet_by_name('Sheet')
italic24Font = Font(size=24, italic=True)
sheet['A1'].font = italic24Font
sheet['A1'] = 'Hello world!'
wb.save('styled.xlsx')
Here is my error message:
Traceback (most recent call last):
File "<pyshell#94>", line 1, in <module>
sheet['A1'].font = italic24Font
AttributeError: can't set attribute
I don't understand why I can't set the attribute. What am I doing wrong?
Edit #1:
What I have tried so far:
using wb.get_sheet_names() to check the sheet name and make sure I'm using the correct name. I am in fact using the correct name.
Edit #2:
I tried viewing the methods available to my object by doing the following:
dir(sheet['A1'].font)
The results I got where:
['UNDERLINE_DOUBLE', 'UNDERLINE_DOUBLE_ACCOUNTING', 'UNDERLINE_SINGLE', 'UNDERLINE_SINGLE_ACCOUNTING', '__add__', '__base__', '__class__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__fields__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__print__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__sub__', '__subclasshook__', '__weakref__', '_key', '_make_key', 'b', 'bold', 'charset', 'color', 'condense', 'copy', 'extend', 'family', 'i', 'italic', 'name', 'outline', 'scheme', 'shadow', 'size', 'spec', 'strike', 'strikethrough', 'sz', 'u', 'underline', 'vertAlign']
I tried running this:
sheet['A1'].font.size=24
But I got this error message:
TypeError: cannot set size attribute
Interesting.
Edit #3:
print(sheet['A1'].style.font.size)
11.0
But when I try:
sheet['A1'].style.font.size=11
I get this error message:
cannot set size attribute