[python-win32] merge

Trent Oliphant trent at oliphant.org
Thu Mar 2 00:07:37 CET 2006


You have to make sure you select a range first.
(There may be other ways to do it, but I use
"Range(Cells(row, column), Cells(row, column))")

Then there are 2 ways you can do it using the Merge() and UnMerge() methods or 
setting the MergeCells Property to True or False

 >>> from win32com.client import Dispatch
 >>> ex = Dispatch("Excel.Application")
 >>> ws = ex.ActiveSheet  #or any other way to get the sheet
 >>> myRange = ws.Range(ws.Cells(1,1),ws.Cells(1,5))
 >>> myRange.MergeCells
False
 >>> myRange.MergeCells = True
 >>> myRange.MergeCells
True
 >>> myRange.MergeCells = False
 >>> myRange.MergeCells
False
 >>> myRange.Merge()
 >>> myRange.MergeCells
True
 >>> myRange.UnMerge()
 >>> myRange.MergeCells
False


It is a bit different than the VBA Syntax, the way I found out how to do it was 
by running makepy on the library and then was able to use the code completion to 
see what was available.  (That is how I have learned most things about pywin)

Trent Oliphant

Jeff.Kelliher at Eirgrid.com wrote:
> Hi,
>  
> I hope you can help me.
>  
> I've been using Python with Access and Excel in conjunction with some 
> power system software for some time now.
>  
> One thing I've been unable to do is merge a defined quantity of cells in 
> Excel with Python - I can merge entire rows and columns but can't 
> determine the correct syntax to merge say five cells etc.
>  
> Can anyone help?
>  
> Best Regards,
>  
> Jeff
> ------------------------------------------------------------------------
> Jeff Kelliher
> Transmission Network Planning, PSDM
> ESB National Grid
> Tel : +353 01 7026813
> Mailto:Jeff.Kelliher at Ngrid.ie <mailto:Jeff.Kelliher at Ngrid.ie>
>  
> 
> 
> *******************************************************************
> This E-mail message (including any attachments) is confidential and 
> intended solely for the use of the individual or entity to whom
> it is addressed. If you have received the message in error, please
> notify security at eirgrid.com.
> 
> The message has been scanned for viruses. ESB National Grid.
> *******************************************************************
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Python-win32 mailing list
> Python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
> 
> 
> ------------------------------------------------------------------------
> 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.375 / Virus Database: 268.1.1/271 - Release Date: 2/28/2006


More information about the Python-win32 mailing list