2
我使用python 2.7的openpyxl来操纵一些工作簿。我为类工作表实现了我自己的方法,除非在尝试访问它们时找不到它们。我在Windows上,我已经将以下代码添加到site-packages/openpyxl/worksheet/worksheet.py。源代码here。Python。添加自定义方法到现有的网站包
class Worksheet(_WorkbookChild):
"""
Library defined methods here.
"""
#My Method.
def hello_world(self):
print "Hello from worksheet."
当我打开的工作簿,并尝试和类工作表的范围之内调用我的函数它将引发错误的,即使它被定义“没有属性”(正确?)。
import openpyxl
wb = openpyxl.load_workbook('helloworld.xlsx')
sheet = wb.get_active_sheet()
sheet.hello_world()
AttributeError: 'Worksheet' object has no attribute 'hello_world'
我是否需要以不同的方式更新现有库?我删除了.pyc,重新编译并仍然收到相同的错误。
如果您使用Python 3,混合选项卡和空格将引发异常。建议**总是**使用空格并相应地配置您的编辑器。 –