[python]關於錯誤/xxx instance has no attribute 'xxx'/'module' object is not callable

今天在寫代碼的時候出現瞭如下兩個錯誤:python

TypeError: 'module' object is not callable

AttributeError: excelChange instance has no attribute 'xlBook'

上網一查,發現第一個錯誤是因爲python中有兩種不一樣的引用方式 import xxx 和 from xxx import *,前者在代碼中引用時須要加上模塊名和具體的方法或屬性,具體方法以下:spa

import catchForm

self.xls = catchForm.catchForm()

而from xxx import *則能夠直接引用:excel

from catchForm import *

self.xls = catchForm()

 

 

解決完第一個錯誤後,立刻又遇到了第二個錯誤,代碼以下:code

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 = ''

上網一查發現:orm

_init_寫錯了,正確的寫法應該是__init__,有兩個下劃線blog

相關文章
相關標籤/搜索