sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
amuven's Avatar

Need Python sample code for open,modify and save the existing excel sheet


Question posted by: amuven (Newbie) on August 21st, 2008 07:27 PM
Hi All,

I'm new to python I have written very basic and small code and I request to help for the following senario.For my project i want to write a python code which should open an already existing Excel sheet which can contain 2 or 3 datasheets and write data in sheet1(first sheet) alone and should not modify the data in rest of the datasheets and save it.

I have heard that people saying it can be done through pyExcelerator or xlrd or xlwt.Since all these things are new to me .. i really don't know how to do it .. :-(

So, I request you all to help me with sample code which will do the above mentioned task as early as possible.

Thanks,
Amudha
2 Answers Posted
Elias Alhanatis's Avatar
Elias Alhanatis August 21st, 2008 08:18 PM
Member - 50 Posts
#2: Re: Need Python sample code for open,modify and save the existing excel sheet

Hi!!

I am not so new in Python , so here is a sample of a script i use to do something like you described:
( Of course you should change all the variables i use and put your own ,
but since i came up with this after some good research ( mainly in the fields of
Visual Basic... ) , i think its a good place for you to start... )

I hope this will help a bit....

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import Dispatch
  2.  
  3. def Create_Analysis_Page(Name,Job,Phone,Month,Analysin  g_Dict,preview=False):
  4.  
  5.     xlApp = Dispatch ("Excel.Application")
  6.     xlWb = xlApp.Workbooks.Open ("C:\\Python25\\Misthos.xlsx")
  7.     xlSht = xlWb.Worksheets (1)
  8.  
  9.     xlSht.Cells(1,2).Value=str(Name)
  10.     xlSht.Cells(2,2).Value=str(Job)
  11.     xlSht.Cells(3,2).Value=int(Phone)
  12.     xlSht.Cells(5,2).Value=str(Month)
  13.  
  14.     try:
  15.         for i in range (9,40):
  16.             xlSht.Cells(i,2).Value=Analysing_Dict[i-8][0]
  17.             xlSht.Cells(i,3).Value=Analysing_Dict[i-8][1]
  18.             xlSht.Cells(i,4).Value=Analysing_Dict[i-8][2]
  19.     except:
  20.         pass
  21.  
  22.     xlSht.Cells(41,4).Value=Analysing_Dict["Totals"][0]
  23.     xlSht.Cells(42,4).Value=Analysing_Dict["Totals"][1]
  24.     xlSht.Cells(43,4).Value=Analysing_Dict["Totals"][2]
  25.  
  26.     #Preview or not....
  27.     if preview:
  28.         xlApp.Visible=1
  29.         xlApp.ActiveWindow.SelectedSheets.PrintPreview()
  30.  
  31.     else:
  32.         #Print The Sheet...
  33.         xlSht.PrintOut()        # This is for printing :)
  34.         xlApp.DisplayAlerts=False   # This is for NOT asking to save changes
  35.         xlApp.Workbooks.Close()
  36.         xlApp.DisplayAlerts=True # This is for bringing back the default behaviour
  37.         xlApp.Quit()
  38.  


Elias
amuven's Avatar
amuven August 26th, 2008 07:21 AM
Newbie - 4 Posts
#3: Re: Need Python sample code for open,modify and save the existing excel sheet

Hi,
It works .... thank you so much .. sorry for late reply :-)
Reply
Not the answer you were looking for? Post your question . . .
197,049 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,049 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Python Contributors