2

I am trying to use win32com to convert multiple xlsx files into xls using the following code:

import win32com.client

f = r"./input.xlsx"
xl = win32com.client.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open(f)
xl.ActiveWorkbook.SaveAs("./somefile.xls", FileFormat=56)

which is failing with the following error:

Traceback (most recent call last):
  File "xlsx_conv.py", line 6, in <module>
    xl.ActiveWorkbook.SaveAs("./somefile.xls", FileFormat=56)
  File "C:\python27\lib\site-packages\win32com\gen_py\00020813-0000-0000-C000-000000000046x0x1x9.py", line 46413, in SaveAs
    , Local, WorkIdentity)
pywintypes.com_error: (-2147352562, 'Invalid number of parameters.', None, None)

Some more details:

I can do other commands to the workbook i.e. wb.Worksheets.Add()and set xl.Visible=True to view the workbook. and even do wb.Save() but can't do a wb.SaveAs()

1
  • I don't think that the error message is complete, and that you obtained it from the code you posted, can you please check carefully? Commented Dec 7, 2016 at 1:14

2 Answers 2

1

The COM exception is due to the missing filename argument as f = r"./input.xlsx" cannot be found. Had you used Excel 2013+, you would have received a more precise exception message with slightly different error code:

(-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Sorry, we couldn't find ./input.xlsx. Is it possible it was moved, renamed or deleted?", 'xlmain11.chm', 0, -2146827284), None)

While your path does work in Python's native context pointing to the directory the called .py script resides, it does not in interfacing with an external API, like Windows COM, as full path is required.

To resolve, consider using Python's built-in os to extract the current directory path of script and concatenate with os.path.join() in the Excel COM method. Also, below uses try/except/finally to properly end the Excel.exe process in background regardless if exception is raised or not.

import os
import win32com.client as win32

cd = os.path.dirname(os.path.abspath(__file__))

try:
    f = os.path.join(cd, "input.xlsx")
    xl = win32.gencache.EnsureDispatch('Excel.Application')
    wb = xl.Workbooks.Open(f)
    xl.ActiveWorkbook.SaveAs(os.path.join(cd, "input.xls"), FileFormat=56)
    wb.Close(True)

except Exception as e:
    print(e)

finally:
    wb = None
    xl = None
Sign up to request clarification or add additional context in comments.

Comments

1

I spent quite a lot of time searching for a proper solution but the only thing I found out is that the script I wrote yesterday today is not working. In addition the same script works on other computers, so I guess this is something broken in the windows/MsExcel/Python environment, but I can't figure out where.

I found a work around to the "SaveAs" problem and it is just to use the "Save" function. Luckily that still works and does not block me from carrying on with my tasks. I hope this help.

import win32com.client as win32
from shutil import copyfile

# you need to define these two:
# src, is the absolute path to the excel file you want to open.
# dst, is the where you want to save as the file.

copyfile(src, dst)

excel   = win32.gencache.EnsureDispatch('Excel.Application')
wb      = excel.Workbooks.Open(PATH_DATASET_XLS)
ws      = wb.Worksheets(DATASET_WORKING_SHEET)

# do some stuff
ws.Cells( 1, 'A' ).Value = "hello" 

# Saving changes
wb.Save() # <- this is the work around
excel.Application.Quit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.