Formatting Strings and Exporting to MS Word

2406
1
06-21-2011 09:33 AM
MikeMacRae
Occasional Contributor III
Hey, does anyone know how to bold a string that you are writing to an MS word doc? I am using the win32com.cleint module and none of the syntaxes seem to work (ie HTML tags, setting bold variables, etc) The help on this module is terrible on the internet.... See comment in the code for where I would like the bold text string.


    wordapp = win32com.client.Dispatch("Word.Application") # Create new Word Object
    wordapp.Visible = 0 # Word Application should`t be visible
    worddoc = wordapp.Documents.Add() # Create new Document Object
    worddoc.PageSetup.Orientation = 1 # Make some Setup to the Document:
    worddoc.PageSetup.LeftMargin = 20
    worddoc.PageSetup.TopMargin = 20
    worddoc.PageSetup.BottomMargin = 20
    worddoc.PageSetup.RightMargin = 20
    worddoc.Content.Font.Size = 10
    worddoc.Content.Paragraphs.TabStops.Add (100)
    # I would like to bold the last concatenated string..."The number of ObjectID records copied....."
    if tifCounter2 < tifCounter:
        worddoc.Content.Text = "Number of Photo Files on the S:drive = " + str(tifCounter) + '\n' + "Number of Ecosite_P records that have an associated ObjectID record = " + str(counter) + '\n' + "Number of Photos copied to new Folder = " + str(tifCounter2) + '\n' + "The number of ObjectID records copied to the new folder does not match the records in the feature class. Please review"
    else:
        worddoc.Content.Text = "Number of Photo Files on the S:drive = " + str(tifCounter) + '\n' + "Number of records that have an associated ObjectID record = " + str(counter) + '\n' + "Number of files copied to new Folder = " + str(tifCounter2) + '\n'
        
    worddoc.Content.MoveEnd
    worddoc.Close() # Close the Word Document (a save-Dialog pops up)
    wordapp.Quit() # Close the Word Application


Tags (2)
0 Kudos
1 Reply
KimOllivier
Occasional Contributor III
There is a good description of programming Word in Python in "Python programming on Win32" by Mark Hammond (O'Reilly) and I found some specific hints in "Writing Excel Macros with VBA" by Roman (O'Reily).
Quote from Mark Please note: Word is hard to work with.

Here are some examples from his book:
http://examples.oreilly.com/9781565926219
0 Kudos