470,619 Members | 1,841 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,619 developers. It's quick & easy.

Excel Manipulation using Python

I was trying to delete rows in an existing .xls file using python. How
do I do that? I was using the following code, it seem to work if I
type in python window, but if I save it in text editor and drage and
drop the .py file, it doesnt work. What am I doing wrong here? Thanks
for your help!

import win32com.client
from time import sleep
excel = win32com.client.Dispatch("Excel.Application")

def Extract():
excel.Visible = 0
workbook=excel.Workbooks.Open('C:\Trial.xls')

i=1
for n in range(1,10):
excel.Rows(i).Select
excel.Selection.Delete
excel.Selection.Delete
i=i+2
workbook.Save()
print "saved"

excel.Quit()
Jun 27 '08 #1
2 1926
Krishna wrote:
I was trying to delete rows in an existing .xls file using python. How
do I do that? I was using the following code, it seem to work if I
type in python window, but if I save it in text editor and drage and
drop the .py file, it doesnt work. What am I doing wrong here? Thanks
for your help!

import win32com.client
from time import sleep
excel = win32com.client.Dispatch("Excel.Application")

def Extract():
excel.Visible = 0
workbook=excel.Workbooks.Open('C:\Trial.xls')

i=1
for n in range(1,10):
excel.Rows(i).Select
excel.Selection.Delete
excel.Selection.Delete
i=i+2
workbook.Save()
print "saved"

excel.Quit()
Several points worthy of note:

1) When you're dealing with Windows filenames, either make
the strings raw -- Open (r"c:\trial.txt") -- or use the other
slashes =-- Open ("c:/trial.xls").

2) You're not actually calling the Select and Delete
methods, merely referencing them. Try .Delete () etc.

3) You're saving the workbook every time round the loop,
but perhaps you knew that. Might prompt you everytime
as you're overwriting, but again, maybe you knew...

TJG
Jun 27 '08 #2
On Apr 18, 11:36*am, Tim Golden <m...@timgolden.me.ukwrote:
Krishna wrote:
I was trying to delete rows in an existing .xls file using python. How
do I do that? I was using the following code, it seem to work if I
type in python window, but if I save it in text editor and drage and
drop the .py file, it doesnt work. What am I doing wrong here? *Thanks
for your help!
import win32com.client
from time import sleep
excel = win32com.client.Dispatch("Excel.Application")
def Extract():
* *excel.Visible = 0
* *workbook=excel.Workbooks.Open('C:\Trial.xls')
* *i=1
* *for n in range(1,10):
* * * * * *excel.Rows(i).Select
* * * * * *excel.Selection.Delete
* * * * * *excel.Selection.Delete
* * * * * *i=i+2
* * * * * *workbook.Save()
* * * * * *print "saved"
* *excel.Quit()

Several points worthy of note:

1) When you're dealing with Windows filenames, either make
the strings raw -- Open (r"c:\trial.txt") -- or use the other
slashes =-- Open ("c:/trial.xls").

2) You're not actually calling the Select and Delete
methods, merely referencing them. Try .Delete () etc.

3) You're saving the workbook every time round the loop,
but perhaps you knew that. Might prompt you everytime
as you're overwriting, but again, maybe you knew...

TJG- Hide quoted text -

- Show quoted text -
Cool, That worked! Thanks for your help TJG!
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Allison Bailey | last post: by
4 posts views Thread by Marc | last post: by
6 posts views Thread by Matthew Wieder | last post: by
9 posts views Thread by [Yosi] | last post: by
3 posts views Thread by timmy | last post: by
16 posts views Thread by Phil Stanton | last post: by
15 posts views Thread by pakerly | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.