0

I have some problem with opening Excel document in web application running under Apache(mod_wsgi)/Windows 2008 Server (there is no problem when the application is running on django developer server - one thread).

My code:

def my_view(request):
   import pythoncom
   from win32com.client import DispatchEx

   pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED)
   xl = win32com.client.dynamic.Dispatch('Excel.Application')
   xl.DisplayAlerts = False
   xl.Visible = 0
   doc = xl.Workbooks.Open("C:\\path\\to\\file.xlsx")
   doc.Saved = True
   ...
   wb.Close(SaveChanges=0)
   xl.Quit()
   pythoncom.CoUninitialize()

Error message:

(-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"Microsoft Office Excel cannot access the file 'C:\path\to \file.xlsx'. There are several possible reasons: The file name or path does not exist. The file is being used by another program. The workbook you are trying to save has the same name as a currently open workbook.", u'C:\Program Files (x86)\Microsoft Office\Office12\ \1033\XLMAIN11.CHM', 0, -2146827284), None)

I know that problem is localized somewhere in threading, but where? I'm using pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED). Maybe changing the server will solve the problem?

Libs: Django 1.2, Apache 2.2 (mod_wsgi), win32com (latest)

I hope somebody can help me.

Thank You, regards.

meme
  • 1
  • 1
  • 1

3 Answers3

3

After hours of researching the exact same problem I found the solution. It has nothing to do with pythoncom / win32com, but with the fact that the apache is running as a service. The solution can be found here:

http://social.msdn.microsoft.com/Forums/en/innovateonoffice/thread/b81a3c4e-62db-488b-af06-44421818ef91

The solution consists of simply making one folder and giving it write permissions from the relevant (apache) user:

64-bit windows - create this folder:

C:\Windows\SysWOW64\config\systemprofile\Desktop

32-bit windows - create this folder:

C:\Windows\System32\config\systemprofile\Desktop
Baz
  • 36,440
  • 11
  • 68
  • 94
ichorev
  • 31
  • 3
0

I'd recommend using the xlrd module to read Excel files from a (threaded) django project, as Office itself is finicky about not being in the main/GUI thread.

Ryan Ginstrom
  • 13,915
  • 5
  • 45
  • 60
  • Unfortunately I can't. I have to manage many macros and do other heavy Excel operations. – meme Jan 26 '11 at 13:09
  • In that case, would a polling solution work? Have a separate process that waits for requests to read Excel files, and processes them in the main (GUI) thread. – Ryan Ginstrom Jan 26 '11 at 14:09
  • I tried. In that case You have to call this separated process from apache process, and the result is the same ;/ – meme Jan 26 '11 at 14:34
0

The solution by ichorev didn't work for me.

But I've added application-group=%{GLOBAL} to the WSGIScriptAlias which solved the problem.

Based on this solution Can't host my wsgi application on other places than /

Like this:

WSGIScriptAlias / "C:/Users/myuser/Documents/Platform/test-temp/test js playground/webproject/webproject/wsgi.py" application-group=%{GLOBAL}