如何解决在Mac上使用xlwings更改字体格式并获得AttributeError
我在名为Book1.xlsx的Excel文件中有一个简单的表 enter image description here
我试图用xlwings获取单元格'A1'的字体信息,如下代码
app = App(visible = False,add_book = False)
wb = app.books.open('Book1.xlsx')
sht = wb.sheets[0]
rng = sht.range('A1')
font_name = rng.api.Font.Name
font_color = rng.api.Font.ColorIndex
font_size = rng.api.Font.Size
print(font_name,font_color,font_size)
通过运行此命令,我得到了AttributeError:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/aeosa/appscript/reference.py in
__getattr__(self,name)
574 try:
--> 575 selectortype,code = self.AS_appdata.referencebyname()[name]
576 except KeyError as e:
KeyError: 'Font'
The above exception was the direct cause of the following exception:
AttributeError Traceback (most recent call last)
<ipython-input-40-265d904476e5> in <module>
----> 1 font_color = rng.api.Font.ColorIndex
2 font_name = rng.api.Font.Name
3 font_size = rng.api.Font.Size
~/opt/anaconda3/lib/python3.7/site-packages/aeosa/appscript/reference.py in
__getattr__(self,name)
575 selectortype,code = self.AS_appdata.referencebyname()[name]
576 except KeyError as e:
--> 577 raise AttributeError("Unknown property,element or command:
{!r}".format(name)) from e
578 if selectortype == kProperty:
579 return Reference(self.AS_appdata,self.AS_aemreference.property(code))
AttributeError: Unknown property,element or command: 'Font'
但是,我可以在Windows 10上完美运行此代码。为什么它在Mac上无法正常运行?
解决方法
print(sht.range('A1').api.font_object.properties.get())
使用此方法,您将获得在Mac OS X中编写xlwings字体API的正确方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。