Excel Python API - python

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.

xlwt and xlrd can read and write Excel files, without using Excel itself:
http://www.python-excel.org/

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.

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

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.

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.

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().

Related

Recording unsaved Excel data with python or automsaving with vba

Using python, I want to continuously record some Excel calculations. The python/excel functions I have used will only read excel data from a saved spreadsheet. Is there a way to read unsaved data?
If not, is there a way to save excel and read the saved data periodically? I tried saving my sheets periodically (with a macro) but this is problematic if I am interacting with the spreadsheet during a save. Instead of saving my spreadsheet, excel saves a copy with a random name. If there is a way to remedy this (maybe some kind of vba error handling) that may solve the problem. Thanks.
Assuming you are using Excel on Windows and not OS X, you can try the win32com.client module, which you can get by installing the Python for Windows package:
http://sourceforge.net/projects/pywin32/
Unfortunately, the available documentation is pretty spotty at best... here's a start:
http://docs.activestate.com/activepython/3.3/pywin32/html/com/win32com/HTML/QuickStartClientCom.html
I should warn you that the COM API isn't very easy to use, so you are really better off sticking with VBA if your workflow requires Excel.
VBA error handling: http://www.cpearson.com/excel/errorhandling.htm

Modifying, recalculating and and extracting results from Excel in Python

I would like to try and make a program which does the following, preferably in Python, but if it needs to be C# or other, that's OK.
Writes data to an excel spreadsheet
Makes Excel recalculate formulas etc. in the modified spreadsheet
Extracts the results back out
I have used things like openpyxl before, but this obviously can't do step 2. Is the a way of recalculating the spreadsheet without opening it in Excel? Any thoughts greatly appreciated.
Thanks,
Jack
You need some sort of UI automation with which you can control a UI application such as Excel. Excel probably exposes some COM interface that you should be able to use for what you need. Python has the PyWin32 library which you should install, after which you'll have the win32com module available.
See also:
Excel Python API
Automation Excel from Python
If you don't necessarily have to work with Excel specifically and just need to do spreadsheet using Python, you might want to look at http://manns.github.io/pyspread/.
you could you pandas for reading in the data, using python to recalculate and then write the new files.
For pandas it's something like:
#Import Excel file
xls = pd.ExcelFile('path_to_file' + '/' + 'file.xlsx')
xls.parse('nyc-condominium-dataset', index_col='property_id', na_values=['NA'])
so not difficult. Here the link to pandas.
Have fun!

Embedding excel into a gui, for a python program

I'm writing a python(3) program that will run on windows only. The program should have a GUI that contains, in part of its window, an embedded excel workbook (and not just its data). Is there a way of doing so with wxPython/PyQt/other?
If not, I found a way of doing that in c#, so I thought of writing the GUI in c# and connect it to python with COM. Can I use IronPython to make a simpler connection? What are the pros and cons of using COM vs using IronPython?
Thanks in advance
One way would be to embed a web browser (QWebView) into the gui and then point it to your Excel Services. The primary issue here, and there are quite a few, is that you'll still need to use something like xlwt to manipulate the data. Unless you have a complex workbook (with macros etc) it might actually be easier to just do something like this using xlwt and xlrd:
value_1 = str(sheet.cell_value(sel,4))
self.lineEdit.setText(value_1)
You can use labels to add column and row names, lineEdits for values, and tabs for sheets. With a little ctrl+v action (or perhaps a "for I in foo") this wouldn't take as long as you might think.

Automating Excel macro using python

I am using python in Linux to automate an excel. I have finished writing data into excel by using pyexcelerator package.
Now comes the real challenge. I have to add another tab to the existing sheet and that tab should contain the macro run in the first tab. All these things should be automated. I Googled a lot and found win32come to do a job in macro, but that was only for windows.
Anyone have any idea of how to do this, or can you guide me with few suggestions.
Excel Macros are per sheets, so, I am afraid, you need to copy the macros explicitly if you created new sheet, instead of copying existing sheet to new one.
You might find that Resolver One is better for what you want - it's a python-scriptable spreadsheet.
Maybe manipulating your .xls with Openoffice and pyUno is a better way. Way more powerful.

Importing Excel sheets, including formulae, into Django

I have an Excel spreadsheet with calculations I would like to use in a Django web application. I do not need to present the spreadsheet as it appears in Excel. I only want to use the formulae embedded in it. What is the best way to do this?
You can control Excel with Python via COM. See this thread: Driving Excel from Python in Windows
It might be a challenge to get this to work reliably as part of a Django app.
In addition to the COM solution, xlrd is cross-platform. That might be more suitable, since I believe Linux is still the most common deployment environment for django. It's also a lighter-weight solution than pyUno.
I think the only thing you can do is use some python/excel mechanism (the only one I could find was this: http://www.python-excel.org/; the tutorial makes me think it might be doable) to read and write from an excel spreadsheet.
You would write to certain cells that would be used by the spreadsheet formulas and then read the results from the formulas from other cells.
Django per-se has nothing to help you with this.
I'll retag your question to include python so that, maybe, someone with Python-excel experience can comment...
You need to use Excel to calculate the results? I mean, maybe you could run the Excel sheet from OpenOffice and use a pyUNO macro, which is somehow "native" python.
A different approach will be to create a macro to generate some more friendly code to python, if you want Excel to perform the calculation is easy you end up with a very slow process.

Categories