By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,510 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

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

P: 4
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
Aug 21 '08 #1
Share this Question
Share on Google+
2 Replies


Elias Alhanatis
P: 56
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,Analysing_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
Aug 21 '08 #2

P: 4
Hi,
It works .... thank you so much .. sorry for late reply :-)
Aug 26 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.