3

I have been running a python script successfully for several months. The script edits a template excel spreadsheet using the win32com commands and then saves the edited workbook as a new .xlsx file.

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)

Without changing anything in the script it no longer works. One day it just stopped working

Here is the error:

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)
1
  • Welcome to the site! Check out the tour and the how-to-ask page for more about asking questions that will attract quality answers. You can edit your question to include more information. Since it's a COM error, I wonder if perhaps a Windows component was updated and win32com is looking for an old version. Did you recently change Excel versions? Commented Sep 29, 2017 at 21:22

2 Answers 2

3

Got it!!!

There was a temporary cache folder "gen_py" that I had to delete. The one that was referenced by the file path in the error.

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

I have no clue why this worked or how the error initially occurred, but everything is fine now.

Sign up to request clarification or add additional context in comments.

1 Comment

I had the same problem and this solution resolved the issue.
0

Running this before saving the file solves the problem.

import os
import tempfile
import shutil

cache_dir = os.path.join(tempfile.gettempdir(), 'gen_py')
if os.path.exists(cache_dir):
    shutil.rmtree(cache_dir)

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.