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

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.

Related

Python - How can I write data from/to an Excel file, then rerun the code with a new input

im currently making a code in Python that goes onto a website, inputs texts, searches for the text, then copies over some data into an excel file. Im using xlsxwriter to do the excel part. However, I want to be able to do another search input and have the information added under the last search in the excel. I am using selenium to scrape the data off chrome.
Ultimately, I want to have my code be able to read one excel file containing a list with the input for a person's name and then occupation. these are the inputs that then give the data searched for on the web, and are stored into a separate excel file.
Im not sure how I can do the searching for inputs off of an excel file or how I can do the add the new data underneath the previously found data.
I hope this makes sense and any tips or tricks would be awesome! Im new to python by the way, so sorry if I ask anything obvious!

Write data with Python into existing excel file keeping it intact as much as possible

We have a rather complicated Excel based VBA Tool that shall be replaced by a proper Database and Python based application step by step.
There will be time of the transition between were the not yet completely ready Python tool and the already existing VBA solution will coexist.
To allow interoperability the Python tool must be able to export the database values into the Excel VBA Tool keeping it intact. Meaning that not only all VBA codes have to work as expected but also Shapes, Special Formats etc, Checkboxes etc. have to work after the export.
Currently a simple:
from openpyxl import load_workbook
wb = load_workbook(r'Tool.xlsm', keep_vba=True)
# Write some data i.e. (not required to destroy the file)
wb["SomeSheet!SomeCell"] = "SomeValue"
wb.save(r"Tool_filled.xlsm")
will destroy the file, i.e. shapes won't work, checkboxes neither. (The resulting file is only 5 MB from originally 8 MB, showing that something went quite wrong).
Is there a way to only modify only the data of an ExcelSheet keeping everything else intact/untouched?
As far I know an Excel Sheet are only zipped .xml files. So it should be possible to edit only the related sheets? Correct?
Is there a more comfortable way as writing everything from scratch to only modify the data of an existing Excel file?
Note: The solution has to work in Linux, so simple remote Excel calls are not an option.

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.

Can Excel with macros(VBA) turned off invoke python script

Scenario: 2 Excel (.xlsx)sheets: INPUT, OUTPUT in same workbook. Calc set to manual.
User inputs numbers (e.g.,sales forecast). Wants to push one key (F9, click Run or a button on the INPUT sheet). Unknown to user a python script is called, executes (say with xlwings or openpyxl) calculates the expected sales for the year and sticks in somewhere in the OUTPUT sheet.
User then prints it out and takes it to his boss for a VIBM (Very Important Business Meeting) while the rest of the folks actually work.
Trivial with VBA involved, but many firms (especially banks) do not allow Excel sheets to run with macros allowed. Someone read that it opens their analyses & systems to hacking.
Is this possible? Any other ways to do it? I could just have a command window open along side, but that gets scary when you consider the users.
Thanks,
C

Module that can help control excel from within python application anything more needed than pywin32?

I have a viewer I built using WXPython. The viewer is basically a browser (built on IE wx.lib.iewin) that loads the txt or htm files I have in a directory and then lets me move through the files sequentially. Instead of having to go to the directory to select the next file to view the viewer/browser has a next button that loads the next file in the queue.
I want to be able to add a new feature that allows me to highlight some text that is visible in the browser and then push a button and have that text passed into a cell in excel.
Lots of things are going to have to happen like I need to be able to find and start a new instance of excel. I need to be able to add a new worksheet and pass some values to populate cells on the worksheet based on the file I am looking at and then if I want to collect some data from the file I want to be able to highlight the data in the viewer and then press a button on the viewer and have the data passed to excel.
I think I am going to start with PyWin32 but I am wondering if there is something else I need but I don't know enough to look for it.
If someone knows of an example where text was piped from a Python application to excel under the users control I would appreciate a pointer in that direction. It is easy enough I think to do this going from the application to a file that gets created (but not displayed) but I am hoping to go from the browser to the excel file so that the user can evaluate their work in progress.
I'd recomend using one of the python excel modules like python-excel.
It works on any OS, and without any other Excel application installed.
http://www.python-excel.org/
Code would look somehting liek this to write to a new xls document, slightly different to open an existing.
import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')
# indexing is zero based, row then column
sheet.write(0,1,'test text')
wbk.save('test.xls')
Hopefully this can get you on the right path, then you'll be able to post more specific questions if you run into problems.
Note: Another option is openpyxl:
http://packages.python.org/openpyxl/tutorial.html
If you're using wxPython (which I assume you are due to the tag), you should look at XLSGrid: http://www.blog.pythonlibrary.org/2011/08/20/wxpython-new-widget-announced-xlsgrid/
If you just want to work with Excel, I would recommend xlwt or xlrd, although you can use PyWin32 to work with it too via COM: http://www.blog.pythonlibrary.org/2010/07/16/python-and-microsoft-office-using-pywin32/

Categories