473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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
2 9863
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
amuven
4
Hi,
It works .... thank you so much .. sorry for late reply :-)
Aug 26 '08 #3

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

Similar topics

2
by: Li Pang | last post by:
Hi, Anybody knows how to close an Excel object and save the excel sheet but without the interruption (confirmation) Thanks in advance
0
by: Sandeep Pulavarty via .NET 247 | last post by:
Hi, I am working on a project in VB.NET and Excel and i wouldlike to add an existing Excel sheet(which is not empty) into mycurrent workbook. I cant use SaveAs because the number of sheetsthat i...
4
by: Marco Aschwanden | last post by:
Hi I would like to 1. import an existing Excel-sheet (a template) 2. and add some data to it 3. and save it under a different name afterwards. To me it seems, that pyExcelerator does not...
1
by: friendlyrohit | last post by:
Hi All !!! Can anybody help me out, how to retrieve a desired column's data from an existing excel sheet through the vb code....... Dim strData As String Dim ts As TextStream ...
1
by: abhiab54 | last post by:
Hi, I want to open already existing excel sheet search for perticular string & write specific value below the cell containing that string. Please help I have limited knowledge of Vb, Perticularly...
1
by: tito | last post by:
Hi All, I am trying to write a code which will convert the Excel sheet document to Pie Diagarm using Perl Script.I have already searched in CPAN which gives the details of...
5
by: HDI | last post by:
Hi, How can I programmatically open a password protected excel-sheet? Thx
2
by: ram712 | last post by:
i need to create/add a copy of an excel sheet to an existing excel sheet consisting of table data so that the new excel sheet renamed automatically. also, the new excel sheet should be blank but...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.