[python]关于错误/xxx instance has no attribute 'xxx'/'module' object is not callable

今天在写代码的时候出现了以下两个错误:

TypeError: 'module' object is not callable

AttributeError: excelChange instance has no attribute 'xlBook'

上网一查,发现第一个错误是由于python中有两种不同的引用方式 import xxx 和 from xxx import *,前者在代码中引用时需要加上模块名和具体的方法或属性,具体方法如下:

import catchForm

self.xls = catchForm.catchForm()

而from xxx import *则可以直接引用:

from catchForm import *

self.xls = catchForm()

 

 

解决完第一个错误后,马上又遇到了第二个错误,代码如下:

class excelChange:
    """一个win32com的封装类,将其所有方法封装在内,
                        统一调用
    """
    def _init_(self,filename = None):#传入文件名参数,如果有的话,没有就把None赋值给filename,自个再新建一个
        self.xlApp = Dispatch('Excel.Application')
        if filename:
            self.filename = filename
            self.xlBook = self.xlApp.Workbooks.Open(filename)
        else:
            self.xlBook = self.xlApp.Workbooks.Add()
            self.filename = ''

上网一查发现:

_init_写错了,正确的写法应该是__init__,有两个下划线

 

posted @ 2014-08-15 16:58  yuexizhuo  阅读(5542)  评论(0编辑  收藏  举报