473,404 Members | 2,137 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,404 software developers and data experts.

Pywin32 Excel question

I posted the following message to the Pywin32 list but if anybody here
can help, it would be appreciated very much.

============================
Hi list,

I have a need to copy 3 rows of data from the top of my Excel
spreadsheet to another location. I would have throught that this
should be very straightforward since I've done a fair amount of
Excel/Python programming. Unforturnately, I am stuck on this one.

The VB Macro says I need to:

Range("1:1,2:2,3:3").Select
Range("A3").Activate
Selection.Copy
Rows("20:20").Select
ActiveSheet.Paste

So, I figure the Python code would be something like:

<xlApp determined already>
1) xlSheet=xlApp.ActiveWorkbook.ActiveSheet
2) xlSel=xlSheet.Range("1:1,2:2,3:3").Select()
3) #xlSel=xlSheet.Range("A3").Activate()
4) xlSel.Copy()
5) xlSheet.Rows("20:20").Select()
6) xlSheet.Paste()

Unfortunately, this doesn't work. After line 2, xlSel becomes "True" -
not a "Selection" and so the code fails at line 4). I am not sure why
I have to do the "Activate" on line 3 but it didn't matter, the code
still fails at line 4.
What am I doing wrong?

Any help is greatly appreciated.

Regards,

Aug 5 '06 #1
2 3780

John Henry wrote:
I posted the following message to the Pywin32 list but if anybody here
can help, it would be appreciated very much.

============================
Hi list,

I have a need to copy 3 rows of data from the top of my Excel
spreadsheet to another location. I would have throught that this
should be very straightforward since I've done a fair amount of
Excel/Python programming. Unforturnately, I am stuck on this one.

The VB Macro says I need to:

Range("1:1,2:2,3:3").Select
Range("A3").Activate
Selection.Copy
Rows("20:20").Select
ActiveSheet.Paste

So, I figure the Python code would be something like:

<xlApp determined already>
1) xlSheet=xlApp.ActiveWorkbook.ActiveSheet
2) xlSel=xlSheet.Range("1:1,2:2,3:3").Select()
3) #xlSel=xlSheet.Range("A3").Activate()
4) xlSel.Copy()
5) xlSheet.Rows("20:20").Select()
6) xlSheet.Paste()

Unfortunately, this doesn't work. After line 2, xlSel becomes "True" -
not a "Selection" and so the code fails at line 4). I am not sure why
I have to do the "Activate" on line 3 but it didn't matter, the code
still fails at line 4.
My first guess is that the True returned in step 2 merely tells you
the requested selection succeeded (the cells on the worksheet
became highlighted). What would happen if you requested 500
columns? Would you get False?

My second guess is that step 4 should be xlSheet.Copy() based
on your using xlSheet.Paste().

For that matter, step 5 doesn't assign the result of the select to a
variable. Perhaps that indicates that the assignment isn't necessary?
Or maybe you should test that the selection succeeded before
attempting to paste?
>

What am I doing wrong?

Any help is greatly appreciated.

Regards,
Aug 5 '06 #2
Somebody on the Pywin32 list helped. The problem is that:

xlSel=xlSheet.Range("1:1,2:2,3:3").Select()

is wrong.

It should be:

xlSel=xlSheet.Range("1:1,2:2,3:3")
xlSel.Select()

Then I can do the rest.

And no, you don't want to do the xlSheet.Copy(). That copies the
entire workbook and you end up with a new workbook.

But you have to do xlSheet.Paste(). Go figure!


me********@aol.com wrote:
John Henry wrote:
I posted the following message to the Pywin32 list but if anybody here
can help, it would be appreciated very much.

============================
Hi list,

I have a need to copy 3 rows of data from the top of my Excel
spreadsheet to another location. I would have throught that this
should be very straightforward since I've done a fair amount of
Excel/Python programming. Unforturnately, I am stuck on this one.

The VB Macro says I need to:

Range("1:1,2:2,3:3").Select
Range("A3").Activate
Selection.Copy
Rows("20:20").Select
ActiveSheet.Paste

So, I figure the Python code would be something like:

<xlApp determined already>
1) xlSheet=xlApp.ActiveWorkbook.ActiveSheet
2) xlSel=xlSheet.Range("1:1,2:2,3:3").Select()
3) #xlSel=xlSheet.Range("A3").Activate()
4) xlSel.Copy()
5) xlSheet.Rows("20:20").Select()
6) xlSheet.Paste()

Unfortunately, this doesn't work. After line 2, xlSel becomes "True" -
not a "Selection" and so the code fails at line 4). I am not sure why
I have to do the "Activate" on line 3 but it didn't matter, the code
still fails at line 4.

My first guess is that the True returned in step 2 merely tells you
the requested selection succeeded (the cells on the worksheet
became highlighted). What would happen if you requested 500
columns? Would you get False?

My second guess is that step 4 should be xlSheet.Copy() based
on your using xlSheet.Paste().

For that matter, step 5 doesn't assign the result of the select to a
variable. Perhaps that indicates that the assignment isn't necessary?
Or maybe you should test that the selection succeeded before
attempting to paste?


What am I doing wrong?

Any help is greatly appreciated.

Regards,
Aug 5 '06 #3

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

Similar topics

2
by: Michael Jordan | last post by:
I'm hoping that someone here can give me some insight into a problem I'm running into with Python, pywin32 and Excel. All-in-all using Python and pywin32 is great but I've run into a strange...
5
by: Egor Bolonev | last post by:
import os print os.listdir("\\\\delta\\public") outputs and import os print os.listdir("\\\\delta")
2
by: Chris Lott | last post by:
Can someone elaborate for me what the pywin32 project is exactly? Is PythonWin a replacement for idle? More to the point, do I need to worry about this as I am learning about Python, since Idle and...
2
by: kogrover | last post by:
ISSUE: COM Excel Sort works with Early Binding, but not Late Binding, but py2exe only does Late Binding I have code similar to this (type from notes, so there may be a typo...) import...
0
by: Andrea Gavana | last post by:
Hi All, I have a very simple python script that tries to put a rectangular shape in a worksheet and then add some text inside that shape. The main problem, is that as usual Excel doesn't like...
1
by: livibetter | last post by:
Hi! I am trying to making an On-Screen Display, which is implemented by wx.Frame. Basically I created a wx.Frame with style like super(OSDBase, self).__init__(parent, id, title, style =...
7
by: vml | last post by:
Hello, I am trying to promote python in my job, my collegue only see matlab and microsoft scripting language. I understood that there willl be no backward compatibility between python 2.x and...
1
by: kernel1983 | last post by:
By reading the doc of pywin32 we can invoke COM like: o = win32com.client.Dispatch("Excel.Application") but is it possible to invoke some GUID directly?
1
by: Ally | last post by:
Hi all, I’m looking to plot charts in Excel from python. After some Googling I’ve found the following code: def plot(x, y, xAxisLog=False, yAxisLog=False): # acquire application object, which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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,...

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.