Python: 使用 openpyxl 模块的字体属性时出现 AttributeError
Python: got an AttributeError when using the font attribue of the openpyxl module
下面的代码来自 Python 辅导网站。但是当 运行 它时我得到了 AttributeError。
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')
错误信息:
> 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
您可以尝试如下,
your_cell.font.size = 24
your_cell.font.italic = True
并查看 doc 以清楚了解如何应用样式。
我有同样的错误:
c.fill = PatternFill('solid', fgColor=RED, bgColor=RED, fill_type='darkGray')
更改它:
c.fill.bgColor = RED
但是什么都没有改变,也有错误。
下面的代码来自 Python 辅导网站。但是当 运行 它时我得到了 AttributeError。
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')
错误信息:
> 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
您可以尝试如下,
your_cell.font.size = 24
your_cell.font.italic = True
并查看 doc 以清楚了解如何应用样式。
我有同样的错误:
c.fill = PatternFill('solid', fgColor=RED, bgColor=RED, fill_type='darkGray')
更改它:
c.fill.bgColor = RED
但是什么都没有改变,也有错误。