无法从 openpyxl 导入导入样式
Cannot import import Style from openpyxl
我已经能够导入 openpyxl 并成功地更改了一些字符的字体颜色,但是当我写下以下行时:
from openpyxl.styles import Style
我收到一条错误消息,提示无法导入样式。我也尝试过小写风格。我觉得这很奇怪,因为以下工作正常:
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl.styles import Fill, Color
不确定您是否将此工作基于教程,但是 Style
在 openpyxl.styles
中对于 openpyxl
版本 2.4.8 不可用。
我在此处导入 openpyxl.styles,然后使用 dir()
函数检查所有可用的选项。
如您所见,Font
、PatternFill
等项目存在,但 Style
不存在。
In [2]: import openpyxl.styles as ops
In [3]: dir(ops)
Out[3]:
['Alignment',
'Border',
'Color',
'DEFAULT_FONT',
'Fill',
'Font',
'GradientFill',
'NamedStyle',
'NumberFormatDescriptor',
'PatternFill',
'Protection',
'Side',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'absolute_import',
'alignment',
'borders',
'builtins',
'cell_style',
'colors',
'differential',
'fills',
'fonts',
'is_builtin',
'is_date_format',
'named_styles',
'numbers',
'protection',
'proxy',
'styleable',
'stylesheet',
'table']
此 表示 Style
已弃用:
这个 section of the openpyxl documentation 有一些关于使用样式的指导。很可能其中描述了您要实现的目标。
我已经能够导入 openpyxl 并成功地更改了一些字符的字体颜色,但是当我写下以下行时:
from openpyxl.styles import Style
我收到一条错误消息,提示无法导入样式。我也尝试过小写风格。我觉得这很奇怪,因为以下工作正常:
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from openpyxl.styles import Fill, Color
不确定您是否将此工作基于教程,但是 Style
在 openpyxl.styles
中对于 openpyxl
版本 2.4.8 不可用。
我在此处导入 openpyxl.styles,然后使用 dir()
函数检查所有可用的选项。
如您所见,Font
、PatternFill
等项目存在,但 Style
不存在。
In [2]: import openpyxl.styles as ops
In [3]: dir(ops)
Out[3]:
['Alignment',
'Border',
'Color',
'DEFAULT_FONT',
'Fill',
'Font',
'GradientFill',
'NamedStyle',
'NumberFormatDescriptor',
'PatternFill',
'Protection',
'Side',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__path__',
'__spec__',
'absolute_import',
'alignment',
'borders',
'builtins',
'cell_style',
'colors',
'differential',
'fills',
'fonts',
'is_builtin',
'is_date_format',
'named_styles',
'numbers',
'protection',
'proxy',
'styleable',
'stylesheet',
'table']
此 Style
已弃用:
这个 section of the openpyxl documentation 有一些关于使用样式的指导。很可能其中描述了您要实现的目标。