Python Forum
win32 package: self-modifying source code???
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
win32 package: self-modifying source code???
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')
When I run it I get a couple of dozen lines of errors, ending with:
f = open(fname,"w") ...Permission denied: 'C:\\...\\Python 3.5\\lib\\site-packages\\win32com\\gen_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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Modifying code cheburashka 1 771 Dec-13-2021, 01:01 PM
Last Post: Kebap
  No Module found in other directory than source code [SOLVED] AlphaInc 1 1,287 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  Generating classes diagram from source code Pavel_47 3 2,655 Oct-01-2021, 03:31 AM
Last Post: Lou
  Python library for win32 console commands eldiener 3 2,133 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  How do I open the Source code of a library? JaneTan 1 1,473 Aug-18-2021, 02:12 AM
Last Post: Larz60+
  Win32\ping.exe windows pops up -very annoying... tester_V 9 2,023 Aug-12-2021, 06:54 AM
Last Post: tester_V
Exclamation binwalk Win32 compile hackstunt 2 2,315 Feb-24-2021, 05:14 PM
Last Post: nilamo
  pyarrow throws oserror winerror 193 1 is not a valid win32 application aupres 2 2,838 Oct-21-2020, 01:04 AM
Last Post: aupres
  Compiling Python 3.8.5 source code results in build error Deepan 0 1,671 Sep-14-2020, 04:11 AM
Last Post: Deepan
  win32 API: Launch Application to RDP session from background process python script rangeshgupta 0 1,508 May-28-2020, 09:41 PM
Last Post: rangeshgupta

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020