473,797 Members | 3,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)

May 17 '06 #1
4 6865
> from win32com.client import constants
sh2.Columns(las tcol).Horizonta lAlignment = constant.xlCent er
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(las tcol).Horizonta lAlignment = constant.xlCent er
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***@augustma il.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(las tcol).Horizonta lAlignment = xlCenter
Traceback (most recent call last):
File "<interacti ve input>", line 1, in ?
NameError: name 'xlCenter' is not defined
Yep, since you have no name 'xlCenter' bound to anything.
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


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

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
20290
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 (MyXL). It uses the worksheet's Application property to make Microsoft Excel visible, to close it, and so on. Using two API calls, the DetectExcel Sub procedure looks for Microsoft Excel, and if it is running, enters it in the Running Object Table. The...
11
4057
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 again, Excel hangs. OR if I open Excel again (say from a desktop icon) before I close Access, Excel hangs. (this has happened for both 97 & 2000 for me) I of course thought that I mustn't be unloading a variable properly.
3
3762
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 on the number of rows/columns in the datagrid, it can be extremely slow (several minutes to populate a 1000x50 spreadsheet). That is understandable, looking at the number of steps to copy over just one row.
4
4392
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 Integer Dim testCount As Boolean testCount = False For t = 0 To UserListView.Items.Count - 1
3
9927
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 (SP1) installed. The error is: System.Runtime.InteropServices.COMException(0x800A03EC): Exception from HRESULT: 0x800A03EC. at Microsoft.Office.Interop.Excel._Worksheet.Paste(Object Destination, Object Link) at...
7
12076
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
20844
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 multiple columns. I'm tried experimenting with Dim rg As Excel.Range = xlSheet.Columns("B, D, G, K, L") but no joy. Thanks in advance Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet
1
2288
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 rows are centering their data. however, the row with three columns, each with images (myimages1-3) are not centering with the rest of the table. Any idea why? Thanks in advance!
2
6423
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 method.... it creates 6 sheets # region Namespaces using System;
0
9537
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10469
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10209
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3750
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.