Note that there are some explanatory texts on larger screens.

plurals
  1. POHow could i generate a well-formatted word file (.DOC) using python?
    primarykey
    data
    text
    <p>I would like to generate a word file using python that has all the formatting generated by MS Word.</p> <p>Please suggest any python modules that can help me achieve this.</p> <p>Code tried for example:</p> <p>Downloaded pywin32-218.win32-py2.7 from <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow">http://sourceforge.net/projects/pywin32/</a> for import win32com.client and trying following example. But it is giving me error as <strong>IndexError: list index out of range in sFileName = sys.argv[1]</strong></p> <pre><code>import sys import time import string import win32com.client # -------------------------------------------------------------------- class CWordAutomate: """Encapsulates a winword com connection""" def __init__( self ): """construct: create OLE connection to winword""" self.m_obWord = win32com.client.Dispatch( "Word.Application" ) self.m_obDoc = self.m_obWord.Documents.Add( ) # create new doc self.m_obWord.Visible = 1 self.m_Sel = self.m_obWord.Selection # get a selection def WriteLine( self, sTxt, sFont, lSize, bBold=0 ): """Write a line to winword""" self.m_Sel.Font.Name = sFont self.m_Sel.Font.Bold = bBold self.m_Sel.Font.Size = lSize self.m_Sel.TypeText( Text=sTxt + "\n" ) # -------------------------------------------------------------------- # - open a file sFileName = sys.argv[1] obFile = file( sFileName, 'r+' ) sContent = obFile.read() obFile.close() lstContent = sContent.splitlines() # - display contents in word obWord = CWordAutomate() obWord.WriteLine( "Content of the file " + sFileName, "Times New Roman", 18, 1 ) for sLine in lstContent: obWord.WriteLine( sLine, "Courier New", 10 ) sLastMsg = time.strftime( "document generated on %c", time.localtime() ) obWord.WriteLine( sLastMsg, "Times New Roman", 14, 0 ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload