Editing/Writing Word-Files from Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Cloutier

    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
  • Peter Hansen

    #2
    Re: Editing/Writing Word-Files from Python

    Daniel Cloutier wrote:[color=blue]
    > is it possible to edit or write Word-files out of a Python-Program?[/color]

    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.pytho n 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

    Comment

    • Alan Kennedy

      #3
      Re: Editing/Writing Word-Files from Python

      [Daniel Cloutier][color=blue]
      > is it possible to edit or write Word-files out of a Python-Program?[/color]

      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 ...

      The World Wide Web Consortium (W3C) is an international community where Member organizations, a full-time staff, and the public work together to develop Web standards.


      #--------- helloworld.xml --- cut here ------------------------
      <?xml version="1.0" encoding='utf-8'?>
      <?mso-application progid="Word.Do cument"?>
      <w:wordDocume nt
      w:embeddedObjPr esent="no"
      w:macrosPresent ="no"
      w:ocxPresent="n o"
      xml:space="pres erve"
      xmlns:aml="http ://schemas.microso ft.com/aml/2001/core"
      xmlns:dt="uuid: C2F41010-65B3-11d1-A29F-00AA00C14882"
      xmlns:o="urn:sc hemas-microsoft-com:office:offi ce"
      xmlns:sl="http://schemas.microso ft.com/schemaLibrary/2003/core"
      xmlns:v="urn:sc hemas-microsoft-com:vml"
      xmlns:w="http://schemas.microso ft.com/office/word/2003/wordml"
      xmlns:w10="urn: schemas-microsoft-com:office:word "
      xmlns:wx="http://schemas.microso ft.com/office/word/2003/auxHint"[color=blue]
      >[/color]
      <o:DocumentProp erties>
      <o:Title>Hell o World</o:Title>
      <o:Author>Ala n</o:Author>
      <o:LastAuthor>A lan</o:LastAuthor>
      <o:Revision>1 </o:Revision>
      <o:TotalTime> 1</o:TotalTime>
      <o:Created>20 04-04-20T15:38:00Z</o:Created>
      <o:LastSaved>20 04-04-20T15:39:00Z</o:LastSaved>
      <o:Pages>1</o:Pages>
      <o:Words>1</o:Words>
      <o:Characters>1 2</o:Characters>
      <o:Company>Alan </o:Company>
      <o:Lines>1</o:Lines>
      <o:Paragraphs>1 </o:Paragraphs>
      <o:CharactersWi thSpaces>12</o:CharactersWit hSpaces>
      <o:Version>11.6 113</o:Version>
      </o:DocumentPrope rties>
      <w:fonts>
      <w:defaultFon ts
      w:ascii="Times New Roman"
      w:cs="Times New Roman"
      w:fareast="Time s New Roman"
      w:h-ansi="Times New Roman"
      />
      </w:fonts>
      <w:styles>
      <w:versionOfBui ltInStylenames w:val="4"/>
      <w:latentStyl es
      w:defLockedStat e="off"
      w:latentStyleCo unt="156"
      />
      <w:style
      w:default="on"
      w:styleId="Norm al"
      w:type="paragra ph"[color=blue]
      >[/color]
      <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="Defa ultParagraphFon t"
      w:type="charact er"[color=blue]
      >[/color]
      <w:name w:val="Default Paragraph Font"/>
      <w:semiHidden/>
      </w:style>
      <w:style
      w:default="on"
      w:styleId="Tabl eNormal"
      w:type="table"[color=blue]
      >[/color]
      <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="NoLi st" 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:doNotEmbedSy stemFonts/>
      <w:proofState w:grammar="clea n" w:spelling="cle an"/>
      <w:attachedTemp late w:val=""/>
      <w:defaultTabSt op w:val="720"/>
      <w:displayHoriz ontalDrawingGri dEvery w:val="0"/>
      <w:displayVerti calDrawingGridE very w:val="0"/>
      <w:useMarginsFo rDrawingGridOri gin/>
      <w:characterSpa cingControl w:val="DontComp ress"/>
      <w:optimizeForB rowser/>
      <w:validateAgai nstSchema/>
      <w:saveInvalidX ML w:val="off"/>
      <w:ignoreMixedC ontent w:val="off"/>
      <w:alwaysShowPl aceholderText w:val="off"/>
      <w:compat>
      <w:footnoteLayo utLikeWW8/>
      <w:shapeLayoutL ikeWW8/>
      <w:alignTablesR owByRow/>
      <w:forgetLastTa bAlignment/>
      <w:doNotUseHTML ParagraphAutoSp acing/>
      <w:layoutRawTab leWidth/>
      <w:layoutTableR owsApart/>
      <w:useWord97Lin eBreakingRules/>
      <w:dontAllowFie ldEndSelect/>
      <w:useWord2002T ableStyleRules/>
      </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

      Comment

      • Hung Jung Lu

        #4
        Re: Editing/Writing Word-Files from Python

        Daniel Cloutier <danielcloutier @yahoo.com> wrote in message news:<c63fgi$7e sh7$1@ID-47444.news.uni-berlin.de>...[color=blue]
        >
        > is it possible to edit or write Word-files out of a Python-Program?[/color]

        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.A dd()
        s = doc.Sentences(1 )
        s.Text = 'This is a test.'
        doc.SaveAs('C:\ \mydoc2.doc')
        app.Quit()

        app = None
        pythoncom.CoUni nitialize()
        --------------------------------------------

        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

        Comment

        • Daniel Cloutier

          #5
          Re: Editing/Writing Word-Files from Python

          Hung Jung Lu wrote:[color=blue]
          > Daniel Cloutier <danielcloutier @yahoo.com> wrote in message news:<c63fgi$7e sh7$1@ID-47444.news.uni-berlin.de>...
          >[color=green]
          >>is it possible to edit or write Word-files out of a Python-Program?[/color]
          >
          >
          > 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.A dd()
          > s = doc.Sentences(1 )
          > s.Text = 'This is a test.'
          > doc.SaveAs('C:\ \mydoc2.doc')
          > app.Quit()
          >
          > app = None
          > pythoncom.CoUni nitialize()
          > --------------------------------------------
          >
          > 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[/color]

          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

          Comment

          Working...