在 Openpyxl 中设置样式
我需要有关在 Openpyxl 中设置样式的建议。
我看到可以设置单元格的 NumberFormat,但我还需要设置字体颜色和属性(粗体等)。有一个 style.py 类,但似乎我无法设置单元格的样式属性,而且我真的不想开始修改 openpyxl 源代码。
有没有人找到解决这个问题的方法?
I need advice on setting styles in Openpyxl.
I see that the NumberFormat of a cell can be set, but I also require setting of font colors and attributes (bold etc). There is a style.py class but it seems I can't set the style attribute of a cell, and I don't really want to start tinkering with the openpyxl source code.
Has anyone found a solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
从 openpyxl 版本 1.5.7 开始,我已经成功应用了以下工作表样式选项...
仅供参考,您可以在
openpyxl/style.py
中找到颜色的名称...我有时会修补来自X11 颜色名称的额外颜色As of openpyxl version 1.5.7, I have successfully applied the following worksheet style options...
FYI, you can find the names of the colors in
openpyxl/style.py
... I sometimes I patch in extra colors from the X11 color names对于 openpyxl 版本 2.4.1 及更高版本,请使用以下代码设置字体颜色:
各种颜色的十六进制代码可以在以下位置找到:
http://dmcritchie.mvps.org/excel/colors.htm
For openpyxl version 2.4.1 and above use below code to set font color:
hex codes for various colors can be found at:
http://dmcritchie.mvps.org/excel/colors.htm
从 openpyxl 2.0 开始,样式是不可变的。
如果您有一个
cell
,您可以(例如)通过以下方式设置粗体文本:cell.style = cell.style.copy(font=cell.style.font.copy(bold=True) )
是的,这很烦人。
As of openpyxl 2.0, styles are immutable.
If you have a
cell
, you can (e.g.) set bold text by:cell.style = cell.style.copy(font=cell.style.font.copy(bold=True))
Yes, this is annoying.
从 openpyxl 2.0 开始,设置单元格样式是通过创建新的样式对象并将它们分配给单元格的属性来完成的。
有多种样式对象:
Font
、PatternFill
、Border
和Alignment
。请参阅文档。要更改单元格的样式属性,首先必须从单元格复制现有样式对象并更改属性值,或者必须使用所需设置创建新的样式对象。然后,将新的样式对象分配给单元格。
将单元格 A1 的字体设置为粗体和斜体的示例:
As of openpyxl 2.0, setting cell styles is done by creating new style objects and by assigning them to properties of a cell.
There are several style objects:
Font
,PatternFill
,Border
, andAlignment
. See the doc.To change a style property of a cell, first you either have to copy the existing style object from the cell and change the value of the property or you have to create a new style object with the desired settings. Then, assign the new style object to the cell.
Example of setting the font to bold and italic of cell A1:
2021 年新更新的 OpenPyXl 中更改字体的方法:
完整代码:
New 2021 Updated Way of Changing FONT in OpenPyXl:
Full Code:
这似乎是一个已经改变了几次的功能。我使用的是 openpyxl 2.5.0,并且我能够以这种方式设置删除线选项:
似乎早期版本(1.9 到 2.4?)在现在的字体上有一个
copy
方法已弃用并引发警告:1.8 以下的版本具有可变字体,因此您可以这样做:
现在会引发错误。
This seems like a feature that has changed a few times. I am using openpyxl 2.5.0, and I was able to set the strike-through option this way:
It seems like earlier versions (1.9 to 2.4?) had a
copy
method on the font that is now deprecated and raises a warning:Versions up to 1.8 had mutable fonts, so you could just do this:
That now raises an error.
就像 openpyxl doc 所说:
我检查了openpyxl源代码,发现:
直到openpyxl 1.8.x,样式都是可变的。它们的属性可以像这样直接分配:
但是从 openpyxl 1.9 开始,样式是不可变的。
要创建新的样式对象,可以直接分配它,或者从现有单元格的样式中复制一个具有新属性的样式,以回答问题为例(请原谅我的中文英语):
单元格的样式包含这些属性:字体,填充、边框、对齐、保护和数字格式。检查
openpyxl.styles
。它们很相似,都应该创建为一个对象,除了number_format,它的值是
string
类型。可以使用一些预定义的数字格式,也可以在字符串类型中定义数字格式。检查
openpyxl.styles.numbers
。Like openpyxl doc said:
I checked openpyxl source code, found that:
Till openpyxl 1.8.x, styles are mutable. Their attribute can be assigned directly like this:
However from of openpyxl 1.9, styles are immutable.
To create a new style object, you can assign it directly, or copy one from an existing cell's style with new attributes, answer to the question as an example(forgive my Chinese English):
A cell' style contains these attributes: font, fill, border, alignment, protection and number_format. Check
openpyxl.styles
.They are similar and should be created as an object, except number_format, its value is
string
type.Some pre-defined number formats are available, number formats can also be defined in string type. Check
openpyxl.styles.numbers
.从 openpyxl-1.7.0 开始,您也可以这样做:
我有几个辅助函数,可以在给定的单元格上设置样式 - 例如页眉、页脚等。
As of openpyxl-1.7.0 you can do this too:
I've got a couple of helper functions which set a style on a given
cell
- things like headers, footers etc.这对我有用(字体颜色+粗体):
This worked for me (font colour + bold font):
您可以定义通用样式,然后将其应用到任何单元格或范围。
定义样式:
应用于单元格。

You can define a common style then you can apply the same to any cell or range.
Define Style:
Apply on a cell.
