Python中的Python设置样式和字体大小

时间:2018-05-03 20:51:58

标签: python excel

这是我的代码,请注意:我正在学习Python:

Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    sheet['A1'].font = italic24Font
AttributeError: can't set attribute

这是我的错误消息:

wb.get_sheet_names()

我不明白为什么我不能设置属性。我做错了什么?

编辑#1:

到目前为止我尝试了什么: 使用dir(sheet['A1'].font) 检查工作表名称并确保我使用正确的名称。我实际上正在使用正确的名称。

编辑#2:

我尝试通过执行以下操作来查看我的对象可用的方法:

['UNDERLINE_DOUBLE', 'UNDERLINE_DOUBLE_ACCOUNTING', 'UNDERLINE_SINGLE', 'UNDERLINE_SINGLE_ACCOUNTING', '__add__', '__base__', '__class__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__fields__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__print__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__sub__', '__subclasshook__', '__weakref__', '_key', '_make_key', 'b', 'bold', 'charset', 'color', 'condense', 'copy', 'extend', 'family', 'i', 'italic', 'name', 'outline', 'scheme', 'shadow', 'size', 'spec', 'strike', 'strikethrough', 'sz', 'u', 'underline', 'vertAlign']

我得到的结果:

sheet['A1'].font.size=24

我试过这个:

TypeError: cannot set size attribute

但我收到此错误消息:

print(sheet['A1'].style.font.size)
11.0

有趣。

编辑#3

sheet['A1'].style.font.size=11

但是当我尝试时:

var re = /(\w\w\d-{1})?\w\w-{1}\w-{1}?/;

我收到此错误消息:

  

无法设置尺寸属性

1 个答案:

答案 0 :(得分:1)

我完全按原样运行你的代码。它在没有AttributeError或任何其他错误的情况下执行。首先,检查您是否具有修改Sheet设置的正确权限。其次,检查您的openpyxl包是否已更新为当前版本(可能旧版本中的语法可能不同)。

通常,当您尝试将值设置为无法设置的属性/属性时会引发AttributeError,因为它没有setter。