473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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("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)

May 17 '06 #1
4 6829
> 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?

May 17 '06 #2
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:
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?

May 18 '06 #3
Lance Hoffmeyer <la***@augustmail.com> writes:
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?

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

May 18 '06 #4
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
May 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet...
11
by: Mr. Smith | last post by:
Hello all, My code can successfully open, write to, format and save several worksheets in a workbook then save it by a given name, close and quit excel. My problem is that if I try and do it...
3
by: | last post by:
Hello, I have a routine in my Windows application that exports the contents of a datagrid to Excel. It is modeled closely after the HowTo example on MSDN: http://tinyurl.com/5g2jm. Depending...
4
by: Powerguy | last post by:
Hi all, I've spent countless hours trying to get this silly EXCEL process to close once I have used it. If anyone can help it would really REALLy be appreciated! Below is my code- Dim t As...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
7
by: TG | last post by:
hi! I am trying to create a sql server table from an excel sheet. Here is the code I have: 'This procedure the xlsx file and dumps it to a table in SQL Server
3
by: Will | last post by:
Can someone help with code to delete multiple columns from an excel spreadsheet? I know which columns I need to delete. The code below will delete a single column but I'm not sure how to delete...
1
by: =?Utf-8?B?ZnJhbmt5?= | last post by:
Hello, I've created a table that has two rows that are span across three columns. The third row has three columns, each with an image. The last row is also span accross three columns. The span...
2
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.