473,911 Members | 5,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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").Act ivate
Selection.Copy
Rows("20:20").S elect
ActiveSheet.Pas te

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

<xlApp determined already>
1) xlSheet=xlApp.A ctiveWorkbook.A ctiveSheet
2) xlSel=xlSheet.R ange("1:1,2:2,3 :3").Select()
3) #xlSel=xlSheet. Range("A3").Act ivate()
4) xlSel.Copy()
5) xlSheet.Rows("2 0: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 3815

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").Act ivate
Selection.Copy
Rows("20:20").S elect
ActiveSheet.Pas te

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

<xlApp determined already>
1) xlSheet=xlApp.A ctiveWorkbook.A ctiveSheet
2) xlSel=xlSheet.R ange("1:1,2:2,3 :3").Select()
3) #xlSel=xlSheet. Range("A3").Act ivate()
4) xlSel.Copy()
5) xlSheet.Rows("2 0: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.R ange("1:1,2:2,3 :3").Select()

is wrong.

It should be:

xlSel=xlSheet.R ange("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").Act ivate
Selection.Copy
Rows("20:20").S elect
ActiveSheet.Pas te

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

<xlApp determined already>
1) xlSheet=xlApp.A ctiveWorkbook.A ctiveSheet
2) xlSel=xlSheet.R ange("1:1,2:2,3 :3").Select()
3) #xlSel=xlSheet. Range("A3").Act ivate()
4) xlSel.Copy()
5) xlSheet.Rows("2 0: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
6020
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 problem with the range Offset property, I'm not getting the correct offset and the returned range is a single cell and not the same size as the original range. For example, when I enter the following lines of code in PythonWin : from...
5
2825
by: Egor Bolonev | last post by:
import os print os.listdir("\\\\delta\\public") outputs and import os print os.listdir("\\\\delta")
2
2211
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 the Windows Installer seem to work fine on my XP box? c
2
3730
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 win32com.client xl = win32com.client.Dispatch("Excel.Application") xl.Visible = False xl.ScreenUpdating = False
0
1585
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 input strings longer than 200 and something characters. So, by just recording a macro in Excel, I tried to append the text in the shape by dividing it in chunks. For example, I tried this little script: #----------------------------------
1
4924
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 = wx.STAY_ON_TOP | wx.FRAME_NO_TASKBAR | wx.TRANSPARENT_WINDOW |
7
4509
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 3.0, does it means that: - my script using pywin32 for the COM layer and scipy for the maths won't work under 3.0
1
1912
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
8796
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 may start application application = Dispatch("Excel.Application") # create new file ('Workbook' in Excel-vocabulary)
0
10038
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11349
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...
0
10921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10541
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
9728
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
7250
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
5940
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4776
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
4341
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.