473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Editing/Writing Word-Files from Python

Hi,

is it possible to edit or write Word-files out of a Python-Program?

thx in advance
daniel
Jul 18 '05 #1
4 4307
Daniel Cloutier wrote:
is it possible to edit or write Word-files out of a Python-Program?


Word uses a proprietary binary format, so you don't really
have as an option the simple "open file, make some changes,
write file" approach you might be picturing.

On the other hand, this *is* Python, so you have alternatives:

1. Use ActiveX and control Word from Python. This is described
well in Mark Hammond's book on Win32 programming with Python,
web pages (use Google), and posts in the comp.lang.python archives.

2. Write HTML or RTF files, which are not proprietary binary
formats. These can be edited in Python and then written back
again. Not sure if there's an RTF library, but "it's just text".

3. Define in more detail what you are actually looking for
("edit" is ill-defined) including the context, and you'll
probably get another three or four ways of doing it.

-Peter
Jul 18 '05 #2
[Daniel Cloutier]
is it possible to edit or write Word-files out of a Python-Program?
If you have access to Office 2003, are feeling brave, and have a lot
of time on your hands, you could create and manipulate the XML
structures that Word 2003 uses.

It thought the group members might find it interesting to see such a
file, so I have exported a "Hello World!" document as XML, and posted
the result below. I had to tidy it up a little, the original came out
all on one line. And I had to add an encoding declaration :-)

In terms of generating such structures, well, everybody has their own
favourite *ML templating language. I'd use TAL or XSLT in "Literal
Result Element as Stylesheet" mode ...

http://www.w3.org/TR/xslt#result-element-stylesheet

#--------- helloworld.xml --- cut here ------------------------
<?xml version="1.0" encoding='utf-8'?>
<?mso-application progid="Word.Document"?>
<w:wordDocument
w:embeddedObjPresent="no"
w:macrosPresent="no"
w:ocxPresent="no"
xml:space="preserve"
xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" <o:DocumentProperties>
<o:Title>Hello World</o:Title>
<o:Author>Alan</o:Author>
<o:LastAuthor>Alan</o:LastAuthor>
<o:Revision>1</o:Revision>
<o:TotalTime>1</o:TotalTime>
<o:Created>2004-04-20T15:38:00Z</o:Created>
<o:LastSaved>2004-04-20T15:39:00Z</o:LastSaved>
<o:Pages>1</o:Pages>
<o:Words>1</o:Words>
<o:Characters>12</o:Characters>
<o:Company>Alan</o:Company>
<o:Lines>1</o:Lines>
<o:Paragraphs>1</o:Paragraphs>
<o:CharactersWithSpaces>12</o:CharactersWithSpaces>
<o:Version>11.6113</o:Version>
</o:DocumentProperties>
<w:fonts>
<w:defaultFonts
w:ascii="Times New Roman"
w:cs="Times New Roman"
w:fareast="Times New Roman"
w:h-ansi="Times New Roman"
/>
</w:fonts>
<w:styles>
<w:versionOfBuiltInStylenames w:val="4"/>
<w:latentStyles
w:defLockedState="off"
w:latentStyleCount="156"
/>
<w:style
w:default="on"
w:styleId="Normal"
w:type="paragraph" <w:name w:val="Normal"/>
<w:rPr>
<wx:font wx:val="Times New Roman"/>
<w:lang
w:bidi="AR-SA"
w:fareast="EN-GB"
w:val="EN-GB"
/>
</w:rPr>
</w:style>
<w:style
w:default="on"
w:styleId="DefaultParagraphFont"
w:type="character" <w:name w:val="Default Paragraph Font"/>
<w:semiHidden/>
</w:style>
<w:style
w:default="on"
w:styleId="TableNormal"
w:type="table"

<w:name w:val="Normal Table"/>
<wx:uiName wx:val="Table Normal"/>
<w:semiHidden/>
<w:rPr>
<wx:font wx:val="Times New Roman"/>
</w:rPr>
<w:tblPr>
<w:tblInd w:type="dxa" w:w="0"/>
<w:tblCellMar>
<w:top w:type="dxa" w:w="0"/>
<w:left w:type="dxa" w:w="108"/>
<w:bottom w:type="dxa" w:w="0"/>
<w:right w:type="dxa" w:w="108"/>
</w:tblCellMar>
</w:tblPr>
</w:style>
<w:style w:default="on" w:styleId="NoList" w:type="list">
<w:name w:val="No List"/>
<w:semiHidden/>
</w:style>
</w:styles>
<w:docPr>
<w:view w:val="print"/>
<w:zoom w:percent="100"/>
<w:doNotEmbedSystemFonts/>
<w:proofState w:grammar="clean" w:spelling="clean"/>
<w:attachedTemplate w:val=""/>
<w:defaultTabStop w:val="720"/>
<w:displayHorizontalDrawingGridEvery w:val="0"/>
<w:displayVerticalDrawingGridEvery w:val="0"/>
<w:useMarginsForDrawingGridOrigin/>
<w:characterSpacingControl w:val="DontCompress"/>
<w:optimizeForBrowser/>
<w:validateAgainstSchema/>
<w:saveInvalidXML w:val="off"/>
<w:ignoreMixedContent w:val="off"/>
<w:alwaysShowPlaceholderText w:val="off"/>
<w:compat>
<w:footnoteLayoutLikeWW8/>
<w:shapeLayoutLikeWW8/>
<w:alignTablesRowByRow/>
<w:forgetLastTabAlignment/>
<w:doNotUseHTMLParagraphAutoSpacing/>
<w:layoutRawTableWidth/>
<w:layoutTableRowsApart/>
<w:useWord97LineBreakingRules/>
<w:dontAllowFieldEndSelect/>
<w:useWord2002TableStyleRules/>
</w:compat>
</w:docPr>
<w:body>
<wx:sect>
<w:p>
<w:pPr>
<w:jc w:val="center"/>
<w:rPr>
<w:sz w:val="40"/>
<w:sz-cs w:val="40"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:sz w:val="40"/>
<w:sz-cs w:val="40"/>
</w:rPr>
<w:t>Hello World!</w:t>
</w:r>
</w:p>
<w:sectPr>
<w:pgSz w:h="16838" w:w="11906"/>
<w:pgMar
w:bottom="1440"
w:footer="720"
w:gutter="0"
w:header="720"
w:left="1800"
w:right="1800"
w:top="1440"
/>
<w:cols w:space="720"/>
</w:sectPr>
</wx:sect>
</w:body>
</w:wordDocument>
#--------- helloworld.xml --- cut here ------------------------

--
alan kennedy
------------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/contact/alan
Jul 18 '05 #3
Daniel Cloutier <da************@yahoo.com> wrote in message news:<c6************@ID-47444.news.uni-berlin.de>...

is it possible to edit or write Word-files out of a Python-Program?


Yes.

(a) You need the win32all modules from Mark Hammond.

(b) To try out the object model of MS Word, press ALT+F11 to bring up
the VBA environment, then F2 to view the object browser. It's a
complicated subject.

-------------------------------------------
import pythoncom
from win32com.client import Dispatch

app = Dispatch('Word.Application')
app.Visible = 1
doc = app.Documents.Add()
s = doc.Sentences(1)
s.Text = 'This is a test.'
doc.SaveAs('C:\\mydoc2.doc')
app.Quit()

app = None
pythoncom.CoUninitialize()
--------------------------------------------

You may want to add better exception handling, otherwise, you may
often have danggling processes when exceptions happen. (You'll then
have to manually kill them from the task manager.)

regards,

Hung Jung
Jul 18 '05 #4
Hung Jung Lu wrote:
Daniel Cloutier <da************@yahoo.com> wrote in message news:<c6************@ID-47444.news.uni-berlin.de>...
is it possible to edit or write Word-files out of a Python-Program?

Yes.

(a) You need the win32all modules from Mark Hammond.

(b) To try out the object model of MS Word, press ALT+F11 to bring up
the VBA environment, then F2 to view the object browser. It's a
complicated subject.

-------------------------------------------
import pythoncom
from win32com.client import Dispatch

app = Dispatch('Word.Application')
app.Visible = 1
doc = app.Documents.Add()
s = doc.Sentences(1)
s.Text = 'This is a test.'
doc.SaveAs('C:\\mydoc2.doc')
app.Quit()

app = None
pythoncom.CoUninitialize()
--------------------------------------------

You may want to add better exception handling, otherwise, you may
often have danggling processes when exceptions happen. (You'll then
have to manually kill them from the task manager.)

regards,

Hung Jung


thanks a lot, yesterday i found out myself that i have to use the
win32com package, but i didn't know how to add some text

greetings
daniel
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Miguel Orrego | last post by:
Hi, Can anybody recommend a good freeware WYSIWYG editor with code view as well? Thanks.
21
by: webmaster | last post by:
Hi all, Without opening a HUGE can of worms, I just wanted to get some ideas of what others are using professionally to create websites commercially. For years I had used Homesite and never...
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
12
by: dino d. | last post by:
hi everyone- my subject pretty much says it all- is there a secure way to do this? the non-secure ways are, as i understand it, to populate a listbox with indices as names, or maybe use a hidden...
4
by: Mike De Petris | last post by:
I am using itextsharp to fill in and flatten some fields from data filled in a web form field and all works quite well. The question now is how to manage the pdf template that has the fields to...
5
by: sara | last post by:
Hi - I have 2 new questions in a new database I'm trying to develop. The first has 6 fields on a table and allows the user to enter them on a form. The form is bound to the table. All 6...
12
by: Trish | last post by:
I just had a bad experience using MS Word as an HTML editor. It bloated more than 200% so I could barely recognize the essential statements; and it introduced more than 14,000 errors. Also,...
10
by: PhilSorum | last post by:
Hi all. I've to edit a txt file, deleting a given word. My approach is to open the file and read it char by char, copying that to a second file. I use a buffer to identify the given word, so if...
1
by: atta | last post by:
hi all i am confronted with a problem where i have to submit a word document to my boss who will edit it and return it to me for printing etc.i am using iis 6 and uploading the file to server from...
1
ganesanji
by: ganesanji | last post by:
hi to all, I am new to php. I have to edit a text file using php. I saw the file system concepts modes. My problem is I want to change a particular text or word in a text file. How to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.