-4
xlapp = Dispatch("Excel.Application")
wkbk = xlapp.Workbooks.Open(filename)

This is the method used to read the excel file but I am unable to get the content of the excel. Can anyone suggest me a better way to handle this

user2982029
  • 84
  • 11
  • 2
    I (and I'm gonna guess everybody else and their mother) have no idea of what you mean by read a file virtually/without physically opening. Be more specific please. – DallaRosa Feb 24 '15 at 07:46
  • sorry for the confusion..... this is how my python code looks like------- – user2982029 Feb 24 '15 at 07:49
  • filename = askopenfilename() print(filename) xlapp.Visible = 0 wkbk = xlapp.Workbooks.Open(filename) – user2982029 Feb 24 '15 at 07:49
  • the .open( filename ) , opens the excel workbook on screen, which I do not want. – user2982029 Feb 24 '15 at 07:50
  • Please edit your question and add the code there! You should format it as code (indentation of four spaces). – Klaus D. Feb 24 '15 at 07:51
  • 2
    I think what he wants is to read the Excel file without actually open an Excel Window. Take a look at xlwings: http://xlwings.org/quickstart/ – Matt Feb 24 '15 at 07:51
  • 1
    Dear user2982029 no matter what, the file has to be opened to be read. Do you mean that you do not want to use Excel? Are you looking for some library that would deal with Excel files? – Tarik Feb 24 '15 at 07:52
  • oh, sorry is that so... I thought I could read an excel file without having to open it, like how we do a fileread in C++ ... likewise do we have a method in PYTHON that can return us values from Cells without having to pop up the execl sheet itself on screen... Im scared if I made sense again, the code: from win32com.client import Dispatch, constants; xlapp = Dispatch("Excel.Application"); filename = askopenfilename() ; xlapp.Visible = 0; wkbk = xlapp.Workbooks.Open(filename) – user2982029 Feb 24 '15 at 07:56
  • @user2982029: the way you put it you'd have to use a seance and a clairvoyant to read the file. The OS is still going to have to direct your harddisk to move the physical parts to read data so Python can do something with the data. – Martijn Pieters Feb 24 '15 at 07:59
  • @user2982029: What you want is for your code to read data without Microsoft Excel to open and produce GUI windows. Those windows are no more or less *physical* (or virtual) than text output Python produces. – Martijn Pieters Feb 24 '15 at 08:01
  • @matt Thanks Matt you understood it. I do not want an excel window to open up. Can you please suggest a way using "from win32com.client import Dispatch, constants" as I am not sure if I am allowed to get xlwings downloaded – user2982029 Feb 24 '15 at 08:01
  • @MartijnPieters: there is a value in leaving "incorrect" terminology after it is disambiguated (OP could clarify whether "virtually" means without a visible Excel window on the screen or without running Excel application at all). I don't think OP is unique, other people might use the same terminology. [The language of the problem may be different from the language of the solution](http://stackoverflow.com/a/14338006/4279). – jfs Feb 24 '15 at 08:21
  • @J.F.Sebastian: agreed; the incorrect terminology can act as a sign-post to answers. But the post does have to be edited to include the clarification. – Martijn Pieters Feb 24 '15 at 08:25
  • @ Sebastian and Martijn : I did not want the excel window to open at all. I only wanted the code to access the data in the excel file. Can you please let me know how would you like me to edit my question. Thanks – user2982029 Feb 24 '15 at 08:32
  • Please let me know if the above posted question makes sense. Thanks – user2982029 Feb 24 '15 at 08:34

2 Answers2

5

xlwings is an excellent library to interact with Excel files from Python.

Opening, reading and writing to Workbooks is as easy as:

>>> from xlwings import Workbook, Sheet, Range, Chart
>>> wb = Workbook()  # Creates a connection with a new workbook
>>> Range('A1').value = 'Foo 1'
>>> Range('A1').value
'Foo 1'

You can install it using:

$ pip install xlwings
Matt
  • 17,290
  • 7
  • 57
  • 71
1

You can try by installing xlrd and xlwt python packages which will helps you to open the Excel workbook virtually. you can refer this links for examples http://www.youlikeprogramming.com/2012/03/examples-reading-excel-xls-documents-using-pythons-xlrd/

sudeep Krishnan
  • 668
  • 1
  • 6
  • 23