473,436 Members | 1,483 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,436 software developers and data experts.

Python-Excel: How to paste selected cells (range) to different location on the same sheet in Excel

Hi there,
I need your help for python <--> excel. I want to paste selected
cells (range) to different location on the same sheet in Excel through
python. I have tried it for a while but could not figure it out. Here
is my sample code:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Visible=1
wb = xl.Workbooks.Add( )
sh=wb.Worksheets(1)
sh.Cells(1,1).Value = "Hello World!"
sh.Cells(3,3).Value = "Hello World!"
sh.Range(sh.Cells(1,1),sh.Cells(3,3)).Select()
sh.Range(sh.Cells(1,1),sh.Cells(3,3)).Copy()
# sh.Range(sh.Cells(4,1),sh.Cells(6,3)).Paste()

The last line of the code does not work.

Thanks you very much.

Ouyang

Aug 4 '05 #1
3 28388
I found the solution for this. It needs to select a new location and
paste from "sh". Thank you for your reading this.

import win32com.client
xl=win32com.client.Dispatch("E*xcel.Application")
xl.Visible=1
wb = xl.Workbooks.Add( )
sh=wb.Worksheets(1)
sh.Cells(1,1).Value = "Hello World!"
sh.Cells(3,3).Value = "Hello World!"
sh.Range(sh.Cells(1,1),sh.Cell*s(3,3)).Select()
sh.Range(sh.Cells(1,1),sh.Cell*s(3,3)).Copy()
sh.Cells(4,1).Select()
sh.Paste()
Ouyang

Aug 4 '05 #2
I think you just need do:
sh.Range(sh.Cells(4,1),sh.Cell*s(6,3)).Value =
sh.Range(sh.Cells(1,1),sh.Cell*s(3,3)).Value

Aug 4 '05 #3
Did you use the excel macro recorder and look at the results to help you
write that program? It seems to bear all the hallmarks of code generated
that way. The macro recorder can be great, but almost always it is
possible to speed up code by altering it afterwards, to condense and
speed up the VBA code. A slightly tighter version of your code is shown
below:

import win32com.client
xl=win32com.client.Dispatch("Excel.Application")
xl.Visible=1
wb = xl.Workbooks.Add( )
sh=wb.Worksheets(1)
sh.Cells(1,1).Value = "Hello World!"
sh.Cells(3,3).Value = "Hello World!"
sh.Range(sh.Cells(1,1),sh.Cells(3,3)).Copy(sh.Cell s(4,1))
sh.Range(sh.Cells(4,1),sh.Cells(6,3)).Select()

The first 7 lines are identical to yours. The way you work with excel in
when entering data and formulae interactively, and so the way the macro
recorder must work, involves lots of selection (or activation for
switching workbooks) which can be slow and often not required when using
code to manipulate excel's objects through COM from an external language
or VBA in excel. Line 7 in your program
"sh.Range(sh.Cells(1,1),sh.Cell*s(3,3)).Select ()" actually does nothing
because the range is specified again in line 8.

The copy method of a range object can take a parameter for a paste
destination. The macro recorder will never generate code like this
because it must record the selection between the copy and the paste
operations.

The only minor drawback of the revised line 7 is that the range selected
after the paste operation is the parameter given to copy not the data
pasted, if this is required, line 8 fixes this.
Aug 9 '05 #4

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

Similar topics

1
by: Alan Sheehan | last post by:
Hi, I am considering scripting a large C++ application. In particular I am interested in test driving and end-user scripting the "application layer" in Python and giving it access to a large...
4
by: Guido van Rossum | last post by:
I'm pleased to announce that the Python Software Foundation (PSF) is now accepting donations from individuals and companies interested in supporting our mission to improve the Python programming...
2
by: Dave Brueck | last post by:
Below is some information I collected from a *small* project in which I wrote a Python version of a Java application. I share this info only as a data point (rather than trying to say this data...
0
by: Jeremy Hylton | last post by:
I am very happy to announce the release of Python 2.3 release candidate 1. The python-dev crew has fixed almost 50 bugs since the beta release last month. You can find the downloads at the usual...
1
by: Jerald | last post by:
Running python 2.3.4 on valgrind (a tool like purify which checks the use of uninitialized memory, etc), gives a lot of errors. See below. jfj@cluster:~/> python -V Python 2.3.4...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 375 open ( -3) / 3264 closed (+26) / 3639 total (+23) Bugs : 910 open ( +3) / 5851 closed (+20) / 6761 total (+23) RFE : 217 open...
1
by: Justin Johnson | last post by:
Hello, I'm trying to build Python 2.5.0 on AIX 5.3 using IBM's compiler (VisualAge C++ Professional / C for AIX Compiler, Version 6). I run configure and make, but makes fails with undefined...
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
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,...
1
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.