[python-win32] Word document with columns and page break.
Little Guy
the_little_guy at gmx.com
Fri Jul 8 06:12:22 CEST 2011
Hi,
I'm trying to create a Python script using win32com that creates a
Word 2003 document. This will become part of a little bigger script
for work. After the document opens up, it is split into two columns,
then text is pasted in, taking up both columns. After this is done,
I'd like to move the insertion point to the end of that page.
Next, I'd like the script to insert a page break, so that a new page
is added to the document, while still keeping the two column
format applied, previously. Once this is done, text, again, will be
pasted in afterwhich the entire document will be sent to any printer,
depending on which is available at the time. After printing the pages,
the document will be closed, without saving.
I have some of the code down, but need the part that moves the
insertion point to the end and inserts a new page as well as the
function to print the file.
Basically, this is what I have, most of which I gathered onlin:
*************************************************
import win32com.client as win32
app = 'Word'
word = win32.gencache.EnsureDispatch('%s.Application' % app)
doc = word.Documents.Add()
word.ActiveDocument.PageSetup.TextColumns.SetCount (2)
word.Visible = True
## Code that pastes text into the document will go here.
*************************************************
## Code that moves insertion point to end of line goes here.
## Code that inserts a new page goes here.
*************************************************
## Code that pastes text into the document will go here.
I've tried reading the Python win32com book, but did not find
what I needed there, have been looking over The Net, but have
had little luck in finding any resource that could help me. I've tried
using VBA Macro Recorder to produce similar code in VBA but I get
stuck when trying to port the code from VBA to Python.
This is VBA Code from the Macro:
**************************************************************
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/7/2011 by Little Guy
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type <> wdPrintView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
With ActiveDocument.PageSetup.TextColumns
.SetCount NumColumns:=2
.EvenlySpaced = True
.LineBetween = False
.Width = InchesToPoints(4.97)
.Spacing = InchesToPoints(0.5)
End With
Selection.InsertBreak Type:=wdPageBreak
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type <> wdPrintView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
With ActiveDocument.PageSetup.TextColumns
.SetCount NumColumns:=2
.EvenlySpaced = True
.LineBetween = False
.Width = InchesToPoints(4.97)
.Spacing = InchesToPoints(0.5)
End With
End Sub
**************************************************************
I've tried browsing Microsoft's COM Automation pages, but it feels like I'm looking
for a needle in a hay stack, as the saying goes. I suppose I could code this
functionality into a VBScript file and then call it from Python, but I'd, much, rather
code everything in Python.
Please excuse the long winded post, and thanks in advance for any assistance.
Regards,
Little Guy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20110708/49017c9e/attachment.html>
More information about the python-win32
mailing list