whitespace in a word doc

Discussion in 'Python' started by Bruce, Jul 15, 2010.

  1. Bruce

    Bruce Guest

    I'm trying to create a word doc using win32com. I don't get the same
    whitespace as when printing the same stuff in the dos window. At the
    terminal I manage to line up the second column like

    apples 5
    pears 7

    I do this by adding whitespace characters to the strings in the first
    column so that their length is equal to that of the longest string in
    the first column.

    I print the excact same string to word. but in the word doc somehting
    happens that messes things up like this :

    apples 5
    pears 7

    Needless to say, this is extremely frustrating. But why does it
    happen, and how can I align the column in word?
    Bruce, Jul 15, 2010
    #1
    1. Advertising

  2. Bruce

    Tim Golden Guest

    On 15/07/2010 08:45, Bruce wrote:
    > I'm trying to create a word doc using win32com. I don't get the same
    > whitespace as when printing the same stuff in the dos window. At the
    > terminal I manage to line up the second column like
    >
    > apples 5
    > pears 7
    >
    > I do this by adding whitespace characters to the strings in the first
    > column so that their length is equal to that of the longest string in
    > the first column.
    >
    > I print the excact same string to word. but in the word doc somehting
    > happens that messes things up like this :
    >
    > apples 5
    > pears 7
    >
    > Needless to say, this is extremely frustrating. But why does it
    > happen, and how can I align the column in word?


    Couple of things which will help us to help you:

    1) Consider what's going on *without* Python: if you take the exact
    same text and spaces and type it into a Word document, does the same
    thing happen? If so, then there is something of a gap in your understanding
    of how Word arranges spaces, especially with proportional fonts.

    2) If the text looks fine when you type it in but dodgy when programmed
    in from Python, post the code here. If there's an issue with the way
    your code is doing what its' doing, we need to see the code to work
    that out. (Usually).

    If you want things to line up in columns in Word, you probably want to
    do one of two things (possibly both): use a fixed-width font, eg Courier
    New;
    use Word tables. If you go the latter route, it can sometimes be easier to
    generate the equivalent HTML and then ask Word to open it directly. Depends.


    TJG
    Tim Golden, Jul 15, 2010
    #2
    1. Advertising

  3. Bruce

    Bruce Guest

    On Jul 15, 9:59 am, Tim Golden <> wrote:
    > On 15/07/2010 08:45, Bruce wrote:
    >
    >
    >
    >
    >
    > > I'm trying to create a word doc using win32com. I don't get the same
    > > whitespace as when printing the same stuff in the dos window. At the
    > > terminal I manage to line up the second column like

    >
    > > apples          5
    > > pears            7

    >
    > > I do this by adding whitespace characters to the strings in the first
    > > column so that their length is equal to that of the longest string in
    > > the first column.

    >
    > > I print the excact same string to word. but in the word doc somehting
    > > happens that messes things up like this :

    >
    > > apples                        5
    > > pears               7

    >
    > > Needless to say, this is extremely frustrating. But why does it
    > > happen, and how can I align the column in word?

    >
    > Couple of things which will help us to help you:
    >
    > 1) Consider what's going on *without* Python: if you take the exact
    > same text and spaces and type it into a Word document, does the same
    > thing happen?


    Yes. Now using courier new. Thanks.
    Bruce, Jul 15, 2010
    #3
  4. On 2010-07-15, Bruce <> wrote:

    > I'm trying to create a word doc using win32com.


    Unfortunately the phrase "word doc" is meaningless.

    Exactly what format file are you trying to generate? For example: Word97 "doc" or the
    new "docx" format?

    > I don't get the same whitespace as when printing the same stuff in
    > the dos window. At the terminal I manage to line up the second column
    > like
    >
    > apples 5
    > pears 7
    >
    > I do this by adding whitespace characters to the strings in the first
    > column so that their length is equal to that of the longest string in
    > the first column.


    Are you just generating an ASCII text file and then opening it in
    word?

    > I print the excact same string to word. but in the word doc somehting
    > happens that messes things up like this :
    >
    > apples 5
    > pears 7
    >
    > Needless to say, this is extremely frustrating. But why does it
    > happen, and how can I align the column in word?


    Why? Because word is using a viable-spaced font and the "dos window"
    uses a fixed-width font.

    If you want any control over the appearance of the document, you'll
    have to either force word to open the file in a fixed-width font, or
    you'll have to generate a file that contains formatting information.
    What you appear to want is a table.

    Generating RTF has worked well for me in the past:

    http://code.google.com/p/pyrtf-ng/
    http://pyrtf.sourceforge.net/

    An enahanced version of pyRTF that supports EMF graphics and scaling
    of graphics is available here:

    http://www.panix.com/~grante/files/python/PyRTF-0.46.tar.gz

    If you want to generate graphics, this might be worth a look

    http://pyemf.sourceforge.net/

    You might also be able to generate HTML and then open that file using
    Word.


    --
    Grant Edwards grant.b.edwards Yow! The Osmonds! You are
    at all Osmonds!! Throwing up
    gmail.com on a freeway at dawn!!!
    Grant Edwards, Jul 15, 2010
    #4
  5. On Thu, 15 Jul 2010 14:10:40 +0000 (UTC), Grant Edwards
    <> declaimed the following in
    gmane.comp.python.general:


    >
    > If you want any control over the appearance of the document, you'll
    > have to either force word to open the file in a fixed-width font, or
    > you'll have to generate a file that contains formatting information.
    > What you appear to want is a table.
    >

    Or something with defined tab stops, and use a tab instead of
    counting spaces.
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Jul 16, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wallace
    Replies:
    0
    Views:
    457
    John Wallace
    Jul 22, 2003
  2. Stephen Witter

    opening a word doc in word not browser

    Stephen Witter, May 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    470
    Stephen Witter
    May 18, 2004
  3. Sharon
    Replies:
    0
    Views:
    591
    Sharon
    Jul 27, 2005
  4. Franck
    Replies:
    1
    Views:
    4,810
  5. Matt
    Replies:
    3
    Views:
    484
    Tor Iver Wilhelmsen
    Sep 17, 2004
Loading...

Share This Page