Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating MS Word documents

micmast's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Belgium
Posts: 137
#1: Mar 17 '09
Hey everybody,

After looking on the internet, I found that a way to create word documents is using the win32com object.

The easy part is
winapp = win32com.client.Dispatch("Word.Application")
windoc = winapp.Documents.Add()

And as far as I have found you have the contents and font part, but how can I add images, tables, headers, footers, ...

Is there an API somewhere, or a book somewhere that would explain to me how this works?

I have found a link to the Word Object Model from Microsoft, but that is a bit of a mess to work with.

http://msdn.microsoft.com/en-us/library/bb244515.aspx

Thx in advance

micmast's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: Belgium
Posts: 137
#2: Mar 17 '09

re: Creating MS Word documents


After further research I found some interesting information that other people might find interesting too.

In your Lib/site-packages/win32com/client there is a tool that is called makepy.py
When you execute this file with python, you get a list with all the COM objects installed on the computer. Select the Word COM object and click OK. It will start generating a python file
if you open that file and look for the class _Application you see a list of available commands

Or

Expand|Select|Wrap|Line Numbers
  1. import win32com.client
  2. wordapp = win32com.client.Dispatch("Word.Application")
  3. wordapp.Visible=1
  4. wordapp.ListCommands()
  5.  
That should open a document with a list of all the commands you have available. Use the previous method to find additional arguments. Allthougt if somebody has any other references feel free to still post them here.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,566
#3: Mar 20 '09

re: Creating MS Word documents


Thanks micmast for the information. I had to pass an integer argument to ListCommands() for it to work though.
Expand|Select|Wrap|Line Numbers
  1. wordapp.ListCommands(1)
Python 2.3.5
Reply