I know there is other ways or modules to read/write xls with python. I have seen Autodesk Dynamo (which use IronPython) can open xls with Microsoft.Office.Interop.Excel related codes. Hence I tried to do similar thing in PyCharm for testing purpose. However, some error pops.
I pick a computer that installed KMSpico for the Office.
That computer installed Python 3.7.x
I have installed pythonnet
I've tried the following code in PyCharm:
import clr
# clr.AddReference("Microsoft.Office.Interop.Excel") # System.IO.FileNotFoundException: Unable to find assembly 'Microsoft.Office.Interop.Excel'.
# I find a dll in C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c
clr.AddReference("Microsoft.Office.Interop.Excel, Culture=neutral, Version=15.0.0.0, PublicKeyToken=71e9bce111e9429c")
import Microsoft.Office.Interop.Excel as Excel
excel = Excel.ApplicationClass()
print(excel.Workbooks) # System.__ComObject
print(dir(excel.Workbooks)) # ['CreateObjRef', 'Equals', 'Finalize', 'GetHashCode', 'GetLifetimeService', 'GetType', 'InitializeLifetimeService', 'MemberwiseClone', 'Overloads', 'ReferenceEquals', 'ToString', '__call__', '__class__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__overloads__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__']
wb = excel.Workbooks.Open(r'C:\Users\user123\Desktop\source\text.xlsx') # AttributeError: '__ComObject' object has no attribute 'Open'
I am not sure why the dll has no Open function