'Python: got an AttributeError when using the font attribue of the openpyxl module
The code below is from a Python tutoring website. But I get the AttributeError when running it.
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')
Error message:
> Traceback (most recent call last):
File "C:\Users\zs\AppData\Local\Programs\Python\Python36\test.py", line 6, in <module>
sheet['A1'].font = italic24Font
AttributeError: can't set attribute
Solution 1:[1]
You can try as below,
your_cell.font.size = 24
your_cell.font.italic = True
and also check the doc for clear understanding on how to apply styles.
Solution 2:[2]
I hava the same errors too:
c.fill = PatternFill('solid', fgColor=RED, bgColor=RED, fill_type='darkGray')
change it:
c.fill.bgColor = RED
But nothing changed, there is a error too.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | chengnian |