22

Does anyone know of a way of accessing MS Excel from Python? Specifically I am looking to create new sheets and fill them with data, including formulae.

Preferably I would like to do this on Linux if possible, but can do it from in a VM if there is no other way.

1
  • 2
    A general comment is that Office file formats themselves are very complicated, but most of the Office tools will read in much simpler formats. Have you considered writing a .csv instead? Commented Jul 14, 2011 at 19:01

6 Answers 6

29

xlwt and xlrd can read and write Excel files, without using Excel itself:

http://www.python-excel.org/

Sign up to request clarification or add additional context in comments.

8 Comments

Unfortunately they do not support formulae
@jamesj: You can read/write formulae. You can't evaluate them. See e.g. secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/examples/…
Thank you, I misunderstood. I thought you could only write them.
I think you can access them too. xlrd is more complex than xlwt.
Yes after looking at this example and trying it out myself with some more non-trivial examples you can. Thanks!
|
6

Long time after the original question, but last answer pushed it top of feed again. Others might benefit from my experience using python and excel.

I am using excel and python quite bit. Instead of using the xlrd, xlwt modules directly, I normally use pandas. I think pandas uses these modules as imports, but i find it much easier using the pandas provided framework to create and read the spreadsheets. Pandas's Dataframe structure is very "spreadsheet-like" and makes life a lot easier in my opinion.

The other option that I use (not in direct answer to your problem) is DataNitro. It allows you to use python directly within excel. Different use case, but you would use it where you would normally have to write VBA code in Excel.

2 Comments

Thanks, both look like interesting approaches. I think I evaluated pandas at the time, but can't remember why I didn't use it.
Pandas does indeed import xlrd and xlwt.
4

there is Python library to read/write Excel 2007 xlsx/xlsm files http://pythonhosted.org/openpyxl/

1 Comment

Looks like exactly what I would have been after at the time :)
4

I wrote python class that allows working with Excel via COM interface in Windows http://sourceforge.net/projects/excelcomforpython/

The class uses win32com to interact with Excel. You can use class directly or use it as example. A lot of options implemented like array formulas, conditional formatting, charts etc.

Comments

3

It's surely possible through the Excel object model via COM: just use win32com modules for Python. Can't remember more but I once controlled the Media Player through COM from Python. It was piece of cake.

2 Comments

I was hoping to do it on linux, which wouldn't give me access to win32com
Yes, I understand. But when it comes to the Microsoft proprietary formats (now open, but practically useless) it's better leave the document generation and processing to the original code. You can always find an earlier version of Excel to run under Wine...
0

Its actually very simple. You can actually run anything from any program. Just see a way to reach command prompt from that program. In case of Excel, create a user defined function by pressing Alt+F11 and paste the following code.

Function call_cmd()
Shell "CMD /C Notepad", vbNormalFocus
End Function

Now press ctrl+s and go back to Excel, select a cell and run the function =call_cmd(). Here I ran Notepad. In the same way, you can see where python.exe is installed and run it. If you want to pass any inputs to python, then save the cells as file in local directory as csv file and read them in python using os.system().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.