excel centering columns

L

Lance Hoffmeyer

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("Excel.Application")
t2.Visible=1
wb = t2.Workbooks.Open (path + xltrack)
sh2 = wb.Worksheets (sheet)
lastcol = sh2.UsedRange.Columns.Count


sh2.Columns(lastcol).HorizontalAlignment = xlCenter
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'xlCenter' is not defined


sh2.Columns(lastcol).HorizontalAlignment = constant.xlCenter

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


sh2.Columns(lastcol).HorizontalAlignment = -4108

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line 455, in __getattr__
return self._ApplyTypes_(*args)
File "C:\Python24\Lib\site-packages\win32com\client\__init__.py", line 446, in _ApplyTypes_
return self._get_good_object_(
com_error: (-2146827864, 'OLE error 0x800a01a8', None, None)
 
J

John Machin

from win32com.client import constants
sh2.Columns(lastcol).HorizontalAlignment = constant.xlCenter
NameError: name 'constant' is not defined

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?
 
L

Lance Hoffmeyer

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
 
B

Ben Finney

Lance Hoffmeyer said:
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

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.
sh2.Columns(lastcol).HorizontalAlignment = xlCenter
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'xlCenter' is not defined

Yep, since you have no name 'xlCenter' bound to anything.
sh2.Columns(lastcol).HorizontalAlignment = constant.xlCenter

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

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

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

Lance Hoffmeyer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top