Skip to main content
 首页 » 编程设计

Python win32com 工作簿.SaveAs 'Invalid number of parameters.' 错误

2024年10月01日7yxwkf

几个月来我一直在成功运行 python 脚本。该脚本使用 win32com 命令编辑模板 excel 电子表格,然后将编辑后的工作簿保存为新的 .xlsx 文件。

results_path = "C:\\Users\\...\\"    
results_title = results_path + input + "_Results.xlsx" 
 
if os.path.exists(template_path): 
    xl= win32com.client.gencache.EnsureDispatch("Excel.Application") 
    xl.Application.DisplayAlerts = False 
 
    xl.Workbooks.Open(Filename= template_path) 
    xl.Application.Cells(2,6).Value = input 
    r = 17 
    for row in y_test: 
        row = str(row) 
        row = row[1:] 
        row = row[:-1] 
        xl.Application.Cells(r,2).Value = row 
        r += 1 
#           xl.Application.CalculateFullRebuild 
#           xl.ActiveWorkbook.SaveAs(Filename = save_title) 
#           time.sleep(20) 
    r = 17 
    for row in prediction: 
        row = str(row) 
        row = row[1:] 
        row = row[:-1] 
        xl.Application.Cells(r,3).Value = row 
        r += 1 
    xl.ActiveWorkbook.SaveAs(Filename = results_title) 

如果不更改脚本中的任何内容,它就不再有效。有一天它停止工作了

这里是错误:

Traceback (most recent call last): 
 
File "<ipython-input-5-aaef40198ed6>", line 1, in <module> 
runfile('C:/Users/Alex/Desktop/Stocks/Python Stock Code/BizNet.py', wdir='C:/Users/Alex/Desktop/Stocks/Python Stock Code') 
 
File "C:\Users\Alex\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile 
execfile(filename, namespace) 
 
File "C:\Users\Alex\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile 
exec(compile(f.read(), filename, 'exec'), namespace) 
 
File "C:/Users/Alex/Desktop/Stocks/Python Stock Code/BizNet.py", line 99, in <module> 
BizNet_test.accuracy_Test(companyInputOrderArray,input,model) 
 
File "C:\Users\Alex\Desktop\Stocks\Python Stock Code\BizNet_test.py", line 125, in accuracy_Test 
xl.ActiveWorkbook.SaveAs(results_title) 
 
File "C:\Users\Alex\AppData\Local\Temp\gen_py\3.5\00020813-0000-0000-C000-000000000046x0x1x9\_Workbook.py", line 284, in SaveAs 
, AccessMode, ConflictResolution, AddToMru, TextCodepage, TextVisualLayout 
 
com_error: (-2147352562, 'Invalid number of parameters.', None, None) 

请您参考如下方法:

知道了!!!

有一个我必须删除的临时缓存文件夹“gen_py”。错误中文件路径引用的那个。

"C:\Users\Alex\AppData\Local\Temp\gen_py\3.5\00020813-0000-0000-C000-000000000046x0x1x9\_Workbook.py" 

我不知道为什么会这样,也不知道错误最初是如何发生的,但现在一切都很好。