excel centering columns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lance Hoffmeyer

    #1

    excel centering columns

    Hey all,

    Having problems centering a column
    Can someone explain how to center a column using python?



    import win32com.client
    import re
    import codecs
    import win32com.client .dynamic
    import time
    import datetime
    from win32com.client .dynamic import Dispatch
    from win32com.client import constants


    t2 = win32com.client .Dispatch("Exce l.Application")
    t2.Visible=1
    wb = t2.Workbooks.Op en (path + xltrack)
    sh2 = wb.Worksheets (sheet)
    lastcol = sh2.UsedRange.C olumns.Count


    sh2.Columns(las tcol).Horizonta lAlignment = xlCenter
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    NameError: name 'xlCenter' is not defined


    sh2.Columns(las tcol).Horizonta lAlignment = constant.xlCent er

    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    NameError: name 'constant' is not defined


    sh2.Columns(las tcol).Horizonta lAlignment = -4108

    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    File "C:\Python24\Li b\site-packages\win32c om\client\__ini t__.py", line 455, in __getattr__
    return self._ApplyType s_(*args)
    File "C:\Python24\Li b\site-packages\win32c om\client\__ini t__.py", line 446, in _ApplyTypes_
    return self._get_good_ object_(
    com_error: (-2146827864, 'OLE error 0x800a01a8', None, None)

  • John Machin

    #2
    Re: excel centering columns

    > from win32com.client import constants[color=blue]
    > sh2.Columns(las tcol).Horizonta lAlignment = constant.xlCent er
    > NameError: name 'constant' is not defined[/color]

    1. So why not try reading the error message and fixing the problem
    instead of thrashing about madly?

    2. You have run makepy on your Excel library, haven't you?

    Comment

    • Lance Hoffmeyer

      #3
      Re: excel centering columns

      I did read the error message. I did not understand why 'constant' was
      not defined. In perl I did not have to run anything like 'makepy' to
      get constants to work. And no, I don't want to go back to perl. Python
      appears to integrate with packages more readily than perl does.

      I was unaware of 'makepy' until you mentioned it. Spent 3 hours searching
      the web and google groups under python excel column | 'centering columns' to
      find a solution. Never saw anything mentioned about 'makepy'.

      Thanks. I will do some searches on 'makepy'.

      Lance






      John Machin wrote:[color=blue][color=green]
      >> from win32com.client import constants
      >> sh2.Columns(las tcol).Horizonta lAlignment = constant.xlCent er
      >> NameError: name 'constant' is not defined[/color]
      >
      > 1. So why not try reading the error message and fixing the problem
      > instead of thrashing about madly?
      >
      > 2. You have run makepy on your Excel library, haven't you?
      >[/color]

      Comment

      • Ben Finney

        #4
        Re: excel centering columns

        Lance Hoffmeyer <lance@augustma il.com> writes:
        [color=blue]
        > import win32com.client
        > import re
        > import codecs
        > import win32com.client .dynamic
        > import time
        > import datetime
        > from win32com.client .dynamic import Dispatch
        > from win32com.client import constants[/color]

        These statements create code objects (some of them module objects) and
        give them names you can use later. They're like assignment statements,
        but a shortcut that also makes the code objects.
        [color=blue]
        > sh2.Columns(las tcol).Horizonta lAlignment = xlCenter
        > Traceback (most recent call last):
        > File "<interacti ve input>", line 1, in ?
        > NameError: name 'xlCenter' is not defined[/color]

        Yep, since you have no name 'xlCenter' bound to anything.
        [color=blue]
        > sh2.Columns(las tcol).Horizonta lAlignment = constant.xlCent er
        >
        > Traceback (most recent call last):
        > File "<interacti ve input>", line 1, in ?
        > NameError: name 'constant' is not defined[/color]

        Yep, since you have no name 'constant' bound to anything.

        You do have the name 'constants' though; you imported it from
        'win32com.clien t'. Perhaps that's what you meant?

        --
        \ "I have one rule to live by: Don't make it worse." -- Hazel |
        `\ Woodcock |
        _o__) |
        Ben Finney

        Comment

        • Lance Hoffmeyer

          #5
          Re: excel centering columns

          Once I imported the EXCEL 10.0 library using
          'makepy utility' everything worked fine.

          And, yes, my typing mistake. Should have been
          'constants' and not 'constant'

          Lance

          Comment

          Working...