在中应用样式时遇到问题-OpenPyXL [英] Trouble with applying Styles in - OpenPyXL

查看:94
本文介绍了在中应用样式时遇到问题-OpenPyXL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置特定的行和列的样式.

I am trying to style specific rows and columns.

worksheet.cell(row=file_row_number, column=1).value = "Hotel ID"
_cell = worksheet.cell("C1")
_cell.style.font.bold = True

它告诉我错误

TypeError: cannot set bold attribute

以前我使用的是XLWT,它有一种非常简单的应用样式的方法,例如您一次定义style变量,然后永远write(),您只需传递style变量即可在该行上应用样式/列

Previously I was using XLWT and it had very easy method of applying styles like you define style variable once and then for ever write() yo can just pass the style variable to apply style on that row/column

OpenPyXL中有没有办法像我上面提到的那样轻松地应用样式?

Is there any way in OpenPyXL to apply style easily as I mentioned above?

推荐答案

此处,您首先应该创建一个Style对象:

As explained here, you should first create a Style object:

from openpyxl.styles import Style, Font

s = Style(font=Font(bold=True))

然后您可以将其应用到您的单元格中,如下所示:

Then you can apply it to your cell like this:

_cell.style = s

这篇关于在中应用样式时遇到问题-OpenPyXL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆