Frage

I'm just running the following code, straight 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 this:

  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

Does anyone have any idea why? I've tried using an xlsx vs. xls file, and changing the file address, and trying multiple examples from that tutorial, and they all give me similar errors, and I have no idea why.

I can get as far as wb = excel.Workbooks.Add() before I get the TypeError: an integer is required warning, and if I try wb = excel.Workbooks.Add, it will run and I won't get the error, but I can't do anything from there on.

Does anyone know what this is? Thanks in advance.

[Edit:]

I tried a word file for comparison and I works fine.

Does anyone know why one of these works and one doesn't?

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:]

Okay, I found the problem is with the Spyder IDE. If I write the same code in Anaconda, it'll work fine. Does anyone know why Anaconda works but Sypder doesn't? I checked the system paths and they're identical, and even trying to execute a .py program in Anaconda doesn't work.

War es hilfreich?

Lösung

I seem to be the only person on the internet with this problem, but my workaround was I used a different spyder python interpretter.

Python interpreter gave me all sorts of errors doing pretty much every win32com excel command, but IPython console works fine. No idea why.

enter image description here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top