Python win32com'无效的参数数量' [英] Python win32com 'Invalid number of parameters'

查看:52
本文介绍了Python win32com'无效的参数数量'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用win32com使用以下代码将多个xlsx文件转换为xls:

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)

更多详细信息:

我可以对工作簿执行其他命令,即 wb.Worksheets.Add()并设置 xl.Visible = True 来查看工作簿.甚至做 wb.Save(),但不能做 wb.SaveAs()

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()

推荐答案

COM异常是由于缺少文件名参数而导致的,因为找不到 f = r"./input.xlsx" .如果您使用的是Excel 2013+,您将收到一条更精确的异常消息,错误代码略有不同:

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,'发生异常.,,(0,'Microsoft Excel',对不起,我们找不到./input.xlsx.它有可能被移动了吗?重命名或删除?",'xlmain11.chm',0,-2146827284),无)

(-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)

虽然您的路径在指向被调用的.py脚本所在目录的Python本机上下文中确实可以使用,但由于需要完整路径,因此它无法与外部API(例如Windows COM)接口.

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.

要解决,请考虑使用Python内置的 os 提取脚本的当前目录路径,并在Excel COM方法中与 os.path.join()并置..另外,无论是否引发异常,下面都使用 try/except/finally 在后台正确结束Excel.exe进程.

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

这篇关于Python win32com'无效的参数数量'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆