473,688 Members | 3,153 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 3798

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
5960
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
2805
by: Egor Bolonev | last post by:
import os print os.listdir("\\\\delta\\public") outputs and import os print os.listdir("\\\\delta")
2
2194
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
3703
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
1561
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
4903
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
4494
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
1900
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
8765
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
8594
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
8947
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...
1
8783
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
8786
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...
1
6454
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 presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4321
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...
0
4547
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2219
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1952
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.