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???
#1
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!
Reply
#2
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.
Reply
#3
Try running from cmd as admin.
There are 3-party lib as python-docx.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  delivery exe without source code py loky62 1 152 Mar-25-2024, 08:35 PM
Last Post: Larz60+
  Algorithm for extracting comments from Python source code Pavel1982 6 413 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,166 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Modifying code cheburashka 1 1,254 Dec-13-2021, 01:01 PM
Last Post: Kebap
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,004 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  Generating classes diagram from source code Pavel_47 3 5,509 Oct-01-2021, 03:31 AM
Last Post: Lou
  Python library for win32 console commands eldiener 3 3,384 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  How do I open the Source code of a library? JaneTan 1 2,226 Aug-18-2021, 02:12 AM
Last Post: Larz60+
  Win32\ping.exe windows pops up -very annoying... tester_V 9 3,127 Aug-12-2021, 06:54 AM
Last Post: tester_V
Exclamation binwalk Win32 compile hackstunt 2 3,716 Feb-24-2021, 05:14 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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