Python Forum

Full Version: win32 package: self-modifying source code???
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to use win32 to edit Microsoft Word documents in Python code. So far I've got this, gleaned from various examples on the web:
import win32com.client as win32

ad = win32.gencache.EnsureDispatch('Word.Application')
pass
When I run it I get a couple of dozen lines of errors, ending with:
Error:
f = open(fname,"w") ...Permission denied: 'C:\\...\\Python 3.5\\lib\\site-packages\\win32com\\gen_py\\__init__.py'
I don't understand a lot of the messages, but it appears that win32com.client gets an error because the gencache object isn't defined, then gets a permission error trying to define the object by changing its own source code.

I've got a couple of questions about this. First, assuming I understand what's happening, why is it OK? Self-modifying code compromises security and maintainability; I haven't seen it done since the '80s, and even then it was only done in RAM. I never dreamed I'd encounter software that modifies its own source code just to initialize.

The second question is: how is it supposed to work? Shared Python packages are installed in the Python program directory, which (in Windows) is in one of the Program Files directories. Of course my program gets a permission error; it isn't allowed to modify program files without administrative privilege, and it darned well shouldn't!
You need to always post the entire error traceback.
A partial traceback rarely gives enough information to diagnose the problem

What you do show, is indicating that (not shown in the code snippet you supply)
a module in gen_py is trying to open a file (I am guessing Word.Application' that does
not exist.
Try running from cmd as admin.
There are 3-party lib as python-docx.
I had used win32com with excel. it generates some files that expose the object model (constants, classes, etc.) to python. It depends on the type of binding you select - i.e. early or late binding. Here you can read more
http://www.icodeguru.com/WebServer/Pytho...2/ch12.htm