Is it possible create/edit an excel macro from python? - python

I'm currently working on a project that requires me to write a (seemingly endless) series of macros to reproduce pivot tables designed by one of our Analysts. The details change, but the code is largely the same from example to example.
I would like to programmaticly generate the vba code based on a handful of options and then add the macro to the given worksheet. Is it possible to create an excel macro with python using win32com or other? Is it possible to create an excel macro from another excel macro? Any other ideas?
Project Background, I have a python script that does the following:
pulls Google Analytic data
does various analysis
writes the results to excel
triggers a pre-written macro to turn the data into a beautifully formatted pivot table
emails it off to people who probably don't actually read it

Yes you can create an Excel macro with another Excel macro. For that to work, you need to tell Excel to Trust Access to the VBA Project Object model. (The setting is found in Macro Options in the Trust Center.) I don't know if you can do it from Python, but if you can, you probably also need to tell Excel it is ok.
For ease of coding, if you are doing this in Excel, add a reference to Micorsoft Visual Basic for Applications Extensibility.
You might also want to check out MZ-Tools 3.0 I find it very helpful for adding default\common code to a project.
On the other hand, your project sounds ripe for code reuse. If the common pivot table code is in one class/module, it is really easy to copy it from one open Excel project to another. (Just click and drag in the Project Explorer window.) You can also export it out to a text file and import it back in to another project later.

Related

Using a macro to insert more macros

I've got some Python code that runs a query on a database that is read-only from my end and, which dumps me an Excel spreadsheet with relevant data that I want for visualization. I have several macros which process the data, create and edit graphs, and let me "filter" a couple different ways (each "filter" is several normal filters - convenient compared to manually selecting/clearing multiple filters).
My Python code generates a new Excel file daily stamped with the date. I would like to insert my suite of macros and run the setup ones automatically on each new file, rather than manually importing and executing them.
Is there a way to use a macro to insert several other macros, run some of them, and insert macro buttons or a dropdown box to run the filtering bit? If not, can it be done (or should it be done) from the Python side when it generates my Excel files?
Edit:
The python code was written by somebody else, and I do not know Python. It prints the data using:
with pd.ExcelWriter(filename) as writer:
df_merged_final.to_excel(writer, sheet_name='Final Data', index=False)
You can collect all your macros in a .bas file and import that whenever needed.
1) collect all your regular macros and put them in a module. You can even write one parent macro that calls all the other child macros, but they all need to be in this module;
2) export the module as a .bas file;
Then whenever you need the macros:
3) import the .bas file to your workbook;
4) your macros are now available in the ribbon of the developer tab which presents them as a dropdown list. If you used a parent macro, you can just click the parent macro to fire all the child macros.
This allows you to work in the source file itself and you can avoid copying large amounts of data to the clipboard. Also serves as a great backup of your most-used macros in case your master Excel file becomes corrupt.

Automating excel reporting and graphs - Python xlsxWriter/xlswings or Ruby axlsx/win32ole

I want to create a program, which automates excel reporting including various graphs in colours. The program needs to be able to read an excel dataset. Based on this dataset, the program then has to create report pages and graphs and then export to an excel file as well as pdf file.
I have done some research and it seems this is possible using python with pandas - xlsxWriter or xlswings as well as Ruby gems - axlsx or win32ole.
Which is the user-friendlier and easy to learn alternative? What are the advantages and disadvantages? Are there other options I should consider (I would like to avoid VBA - as this is how the reports are currently produced)?
Any responses and comments are appreciated. Thank you!
If you already have VBA that works for your project, then translating it to Ruby + WIN32OLE is probably your quickest path to working code. Anything you can do in VBA is doable in Ruby (if you find something you can't do, post here to ask for help).
I prefer working with Excel via OLE since I know the file produced by Excel will work anywhere I open it. I haven't used axlsx but I'm sure it's a fine project; I just wouldn't trust that it would produce working Excel files every time.

Excel Python API

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

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