Python openpyxl字体TypeError

huangapple go评论83阅读模式
英文:

Python openpyxl Font TypeError

问题

在我的程序中,我正在使用openpyxl模块的最新版本(3.1.2),并像这样设置单元格的字体:

from openpyxl.styles.fonts import Font

并设置单元格的字体如下:

main.cell(row=row, column=col).font = Font(color='00808080')

然而,从今天开始,它停止工作了。它报错:

TypeError: Font.__init__() got an unexpected keyword argument 'color'

如果我查看Font类,我可以清楚地看到它不是意外的。

在今天之前,它一直正常工作。

我检查了一下,没有任何更改,版本保持不变,代码也保持不变。

我甚至尝试重新安装openpyxl包,并安装一些早期版本,但都没有帮助。

英文:

So in my program i was using last version(3.1.2) of openpyxl module:

from openpyxl.styles.fonts import Font

and setting font of cells like this:

main.cell(row=row, column=col).font = Font(color='00808080')

However, from today it stopped working. It gives an error:

TypeError: Font.__init__() got an unexpected keyword argument 'color'

If i go to Font class i can clearly see that it is not unexpected.

It was working perfectly fine before today.

I checked, nothing changed, versions stayed the same, code stayed the same.

I even tried to reinstall openpyxl package, and install a few earlier versions, but that didnt help.

答案1

得分: 0

从openpyxl.styles.fonts导入Font

从openpyxl.drawing.text导入Font

所以我添加了对第一个的导入Font as ...,然后它开始工作。

英文:

So i was simultaniously importing

from openpyxl.styles.fonts import Font

and

from openpyxl.drawing.text import Font

So i added to first one import Font as ... and it started working

答案2

得分: 0

以下是翻译好的部分:

可能的错误是由于对openpyxl模块或相关依赖项的更新引起的。您可以尝试的一件事是明确从openpyxl.styles包中导入Font类,而不是从openpyxl.styles.fonts,像这样:

from openpyxl.styles import Font
main.cell(row=row, column=col).font = Font(color='00808080')

如果这不起作用,您还可以尝试使用不同格式来指定颜色,例如RGB元组或十六进制代码,像这样:

main.cell(row=row, column=col).font = Font(color='FF808080')

或者,您可以尝试使用不同的字体属性来实现所需的效果,例如设置字体名称或大小。

英文:

It's possible that the error is caused by an update to the openpyxl module or a related dependency. One thing you can try is to explicitly import the Font class from the openpyxl.styles package instead of from openpyxl.styles.fonts, like this:

from openpyxl.styles import Font
main.cell(row=row, column=col).font = Font(color='00808080')

If that doesn't work, you could also try specifying the color using a different format, such as an RGB tuple or hex code, like this:

main.cell(row=row, column=col).font = Font(color='FF808080')

Alternatively, you could try using a different font attribute to achieve the desired effect, such as setting the font name or size.

答案3

得分: -1

"from openpyxl.styles import Font" 可以翻译为:"从 openpyxl.styles 导入 Font"。

英文:
from openpyxl.styles import Font

huangapple
  • 本文由 发表于 2023年5月10日 17:13:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76216723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定