Win32com in Python Spyder Console Fails

I just run the following code, right from this documentation / tutorial .

import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
wb.SaveAs('add_a_workbook.xlsx')
excel.Application.Quit()

And got the following:

  execfile(filename, namespace)
  File "C:/Users/Username/Desktop/script.py", line 106, in <module>
    wb = excel.Workbooks.Add()
  File "C:\Users\Username\AppData\Local\Temp\gen_py\2.7\00020813-0000-0000-C000-000000000046x0x1x7\Workbooks.py", line 34, in Add
    ret = self._oleobj_.InvokeTypes(181, LCID, 1, (13, 0), ((12, 17),),Template
TypeError: an integer is required

Can anyone understand why? I tried using the xlsx vs. file xls and changed the file address and tried several examples from this tutorial, and they all give me similar errors, and I have no idea why.

I can get before wb = excel.Workbooks.Add()I get a warning TypeError: an integer is required, and if I try wb = excel.Workbooks.Add, it will start and I will not get an error, but I can’t do anything from there to.

Does anyone know what this is? Thanks in advance.

[Edit:]

I tried a text file for comparison and it works fine.

Does anyone know why one of these works and the other not?

word = win32.Dispatch('Word.Application')
word.Documents.Open('C:\Users\username\Desktop\test.docx')

excel = win32.Dispatch('Excel.Application')
excel.Workbooks.Open('C:\Users\username\Desktop\output.xlsx')

[Edit 2:]

, IDE Spyder. Anaconda, . - , Anaconda, Sypder ? , , .py- Anaconda .

+4
1

, , , spyder python.

Python interpreter , win32com excel, IPython console . , .

enter image description here

+3

Source: https://habr.com/ru/post/1540791/


All Articles