passing the return caharacter to word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dittytwo
    New Member
    • Jan 2008
    • 10

    passing the return caharacter to word

    Howdy there
    I have the below code
    <code>
    import win32com.client ,pythoncom
    pythoncom.CoIni tializeEx(pytho ncom.COINIT_APA RTMENTTHREADED)
    wordapp = win32com.client .Dispatch("Word .Application") # Create new Word Object
    wordapp.Visible = 1 # Word Application should`t be visible
    worddoc = wordapp.Documen ts.Add() # Create new Document Object
    worddoc.Content .Text = "=rand(200,99)" +"/r"
    worddoc.Content .MoveEnd
    worddoc.SaveAs( "c:\wiggle.doc" )
    worddoc.Close() # Close the Word Document (a save-Dialog pops up)
    wordapp.Quit() # Close t
    pythoncom.CoUni nitialize()
    </code>

    the problem i have is that it
    saves the word doc with "=rand(200, 99)/r"
    i think that i am expecting too much from the com or word to action this statement in word.
    as pressing return after =rand(x,x) gives you "The quick brown fox jumps over the lazy dog." quite a few times :D hence filling up a document for testing purposes...

    D2
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    I'm pretty sure that in Windows the "return" character that you speak of is \r\n. You could alternately use os.linesep to return the end of line character for your OS. Also, C:\wiggle.doc I think should be C:\\wiggle.doc since \ is the escape character in Python. And when you say that visible = 1 should make the application non-visible that is a lie. visible =0 would be invisible, visible = 1 is visible.

    Also, for your code tags use [ ] square brackets... you were close! ;)j
    Also also, your opening code tag can read code=python to get syntax highlighting!

    P.S. I tried your code and had no luck getting Word to automatically generate the text... I think the problem lies within auto completion; however I've never messed with Office com objects before so who knows...

    Your code only slightly modified:
    [code=python]
    import os, win32com.client , pythoncom
    pythoncom.CoIni tializeEx(pytho ncom.COINIT_APA RTMENTTHREADED)
    wordapp = win32com.client .Dispatch("Word .Application")
    wordapp.Visible = 1
    worddoc = wordapp.Documen ts.Add()
    worddoc.Content .Text="=rand(20 0,99)" + os.linesep
    worddoc.Content .MoveEnd()
    worddoc.SaveAs( "C:\\wiggle.doc ")
    worddoc.Close()
    wordapp.Quit()
    pythoncom.CoUni nitialize()
    [/code]

    Comment

    Working...