Run in python the equivalent of a VBA code - python

Is there any way to run in Pyhton the equivalent of this Excel VBA code:
For i= 1 to 10
'do some stuff
'Refresh calculations in order to check the outcome
Next i
In other words: is it possible to refresh sheet formula results from within python, then rinse and repeat ? I tried saving as excel file and re-opening after each iteration, but it is too time consuming to begin with.

You can run VBA without Excel. Just save the code into a file and use the windows built in command vbscript.
https://www.faqforge.com/windows/run-vbscript-windows/
You can run the command via subprocess.
If you want to run code in Excel, you have to write an Excel addon in python.
There is an open source library called xlwings: https://www.xlwings.org/
(there is also a premium version of xlwings but you do not need them)

Related

Python: how to start Excell add-in, request data, and save?

I have an excel add-in (morningstar) that allows me to request and download data by entering a function with some parameters in cell A1. I have a thousand values of parameters that I need to get the data for so I would want to automate this process. I can (sort of) do that with VBA but it takes forever, and crashes many times. I would like to do this in Python. The logic which I am doing in VBA is as follows:
Open the xlsx file (workbook)
Refresh Excel so to make sure the Add-in is loaded and data is downloaded
Loop through sheets and save as csv files
I tried opening the excel file with python using the subprocess package. But I cannot make it until the data is downloaded. I attached 2 photos of the download process: (i)when the function in cell A1 is executed, the cell display "Processing..." . It would take a few seconds to a few minutes depending on the size of the data. when done, the data looks like in the second photo.
So my questions are:
How to ask python to start excel and load the add-in?
Execute the fuction in cell A1, or at least refresh excel to make sure the function runs.
Instead of asking python to wait for an exact time, how do I ask python to check if the data is downloaded?
Ideally, (I think) doing this without opening excel would save a lot of time, but not a major issue.

Synchronization using python code based on an excel file to JIRA

I am using python package 'jira' for establishing connection with the jira. I basically use the information from excel file and create tickets automatically in JIRA based on the excel information. Sometime there might be changes in the excel information for the same ticket in which case I need to run the code manually. So I would like to know if it is possible to do this automatically whenever there is a change in the excel file.
I assume you are only interested in filing new tickets. i.e. adding new rows in excel sheet.
Two options:
Run your code in a forever loop with sleep.
Have your code run by a cron.
Now, you can maintain the hash(md5 or sha256) of your file, and write the hash in some file on host machine if you are not using a database.
Your code has to read from this file, and calculate fresh hash of that excel file. If they are not same, means something has changed in your file.
Now, you also need to maintain till what row you have created the jira tickets. You can write this information also in some file.

How does one deal with refresh delay when calling python from VBA using .txt as input and output?

I am using VBA-macros in order to automate serveral data processing steps in excel, such as data reduction and visualization. But since excel has no appropriate fit for my purposes I use a python script using scipy's least squares cubic b-spline function. The in and output is done via .txt files. Since I adjusted the script from a manual script I got from a friend.
VBA calls Python
Call common.callLSQCBSpline(targetrng, ThisWorkbook) #calls python which works
Call common.callLoadFitTxt(targetrng, ThisWorkbook) # loads pythons output
Now the funny business:
This works in debug mode but does not when running "full speed". The solution to the problem is simply wait for the directory were the .txt is written to refresh and to load the current and not the previous output file. My solution currently looks like this:
Call common.callLSQCBSpline(targetrng, ThisWorkbook)
Application.Wait (Now + 0.00005)
Call common.callLoadFitTxt(targetrng, ThisWorkbook)
This is slow and anoying but works. Is there a way to speed this up? The python script works fine and writes the output.txt file properly. VBA just needs a second or two before it can load it. The txts are very small under 1 kB.
Thanks in advance!

Excel Big Data Calculation (PCA...)

I have to do some calculation on data stored in excel for my internship.
I am supposed to aggregate market datas (50 assets over 15 years) and do a Principal Component Analysis over the aggregated datas.
For the moment I have the market data in a worksheet, I save it as a tabulation separated text (like csv but with tabulation instead of commas). Then I read it with R and use some powerfull package to do the PCA. Finally, with R I create another tabulation separated text and read it trough excel. I now have datas and results in excel and I can plot everything I want.
The problem is that the process is not enough automated for my colleagues.
As they said, they want a button in excel which launch the PCA when clicked.
I've tried to install some Excel Package (Rexcel) which allow to use R function directly in excel. It's not working (a server problem) and not well documented. So I'm trying to find others way to do big calculation directly in excel. It seems that there is the same kind of package to use Python in Excel. I've also heard about other powerfull langage compatible with excel. The problem is that I can't install what I want on my computer (yeah I have to call an IT guy for every package I want to install...), so it already took me 2/3 days to try the R solution. This is also why i'm looking for a simple solution, my colleagues won't have 2/3 days to install some excel package to use my macro...
So i'm here to ask: what would be the easiest way to do PCA, using tools from other languages, directly in excel ?
Many thanks in advance
You can use the very handy executable Rscriptto launch automatically your R scripts.
Within VBA you create a macro where you type something like this :
retVal = Shell(MY_RSCRIPT_BAT, vbNormalFocus) ## vba code here
I assume that you can call a VBA macro from a button.
your MY_RSCRIPT_BAT , is .bat file where you type something like:
#echo off
C:
PATH R_PATH;%path%
cd DEMO_PATH
Rscript your_pca_script.R
exit

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

Categories