1

I have a PY3 script that uses Win32COM to instantiate a new Excel instance and attempt to open an existing .xlsx file - boiled down, this fails when called via Tomcat/CGI on Windows:

sys.stdout.write("Content-Type: text/html\n\n")
excel = DispatchEx('Excel.Application')

dir = r'C:\temp'
s = 'test.xlsx'
sfile = os.path.join(dir, s)

try:
    wbS = excel.Workbooks.Open(sfile)
except pythoncom.com_error as error:
    print("exception details: {0}".format(error))
    exit(1)

Action fails with:

exception details: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Microsoft Excel cannot access the file 'C:\\temp\\test.xlsx'. There are several possible reasons:\n\n• The file name or path does not exist.\n• The file is being used by another program.\n• The workbook you are trying to save has the same name as a currently open workbook.", 'xlmain11.chm', 0, -2146827284), None)

Fails when Tomcat runs either as Administrator or local account, both having Full Control access to the existing file. EnsureDispatch, Dispatch and DispatchEX do not yield success. No other application is accessing/locking the file. Running from within a cmd succeeds, suggesting that something is lost at the CGI level.

(Using COM allows for copying of worksheets between workbooks and other activities not supported by XLSWriter, et.al.)

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
jdavisftc
  • 11
  • 1
  • also - even if the .xlsx is located in the webapp's hierarchy, same error – jdavisftc Feb 14 '17 at 20:05
  • this did it, for anyone that hits similar: https://stackoverflow.com/questions/4803850/win32com-excel-django-apache-problem – jdavisftc Feb 14 '17 at 21:57
  • Possible duplicate of [win32com + Excel + Django + Apache = problem](http://stackoverflow.com/questions/4803850/win32com-excel-django-apache-problem) – chwarr Feb 14 '17 at 23:07

0 Answers0