Is there anyway we can call an excel addin using python? - python

Is there anyway we can call an excel addin using python?
I would like to call a user-defined add-in which is present in excel using a python program. I need a freeware I think pyxll requires license.

You can try xlwings if you like to use python inside your excel.
Or try to dispatch excel with win32com
excel = win32com.client.gencache.EnsureDispatch ("Excel.Application")
I think that you'd have to save your add-in as vba project (How to Use Your Excel Add-In Functions in VBA.
Generally nearly everything that can be done via VBA could be done with python and dispatched excel, so VBA Object Model reference is a good documentation on it.

Related

How to customize the action of a button in excel sheet using xlsxwriter

I am wondering to use xlsxwriter to control my excel sheet instead of using old fashioned way by using VBA. I need a button to trigger some actions. What I read from the official documentation is
workbook.add_vba_project('./vbaProject.bin')
worksheet.insert_button('B3', {'macro': 'say_hello',
'caption': 'Press Me'})
I don't know how to define my own function as a macro and I don't know how to generate "vbaProject.bin". Is there anyway to write a macro in format like python function and directly assign it to the button?
If I must include the macro in vbaProject.bin, how can I do that? Hope it is not something like vba.
So you have many ways to control and automate your files:
You can use vba
You can use xlsxwriter which uses python syntax
You can use a library called PyXLL https://www.pyxll.com/docs/userguide/macros.html . It seems you are able to write a macro using python syntax but i haven't tried it, you could have a look as it might be what you are looking for.
About your question on how to inject a macro to your excel using xlsxwriter have a look at my answer here, i think it is quite detailed:
Add dataframe and button to same sheet with XlsxWriter
By the way vba is not hard to learn and there is a lot of information online. If you don;t want to write the code manually you can record the macro by pressing record, do your thing and stop the recording. Then you will have the code which you can inject it to your file (look at my tutorial above)

Update an Excel sheet in real time using Python

Is there a way to update a spreadsheet in real time while it is open in Excel? I have a workbook called Example.xlsx which is open in Excel and I have the following python code which tries to update cell B1 with the string 'ID':
import openpyxl
wb = openpyxl.load_workbook('Example.xlsx')
sheet = wb['Sheet']
sheet['B1'] = 'ID'
wb.save('Example.xlsx')
On running the script I get this error:
PermissionError: [Errno 13] Permission denied: 'Example.xlsx'
I know its because the file is currently open in Excel, but was wondering if there is another way or module I can use to update a sheet while its open.
I have actually figured this out and its quite simple using xlwings. The following code opens an existing Excel file called Example.xlsx and updates it in real time, in this case puts in the value 45 in cell B2 instantly soon as you run the script.
import xlwings as xw
wb = xw.Book('Example.xlsx')
sht1 = wb.sheets['Sheet']
sht1.range('B2').value = 45
You've already worked out why you can't use openpyxl to write to the .xlsx file: it's locked while Excel has it open. You can't write to it directly, but you can use win32com to communicate with the copy of Excel that is running via its COM interface.
You can download win32com from https://github.com/mhammond/pywin32 .
Use it like this:
from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
wb=xlApp.Workbooks.Item("MyExcelFile.xlsx")
ws=wb.Sheets("MyWorksheetName")
At this point, ws is a reference to a worksheet object that you can change. The objects you get back aren't Python objects but a thin Python wrapper around VBA objects that obey their own conventions, not Python's.
There is some useful if rather old Python-oriented documentation here: http://timgolden.me.uk/pywin32-docs/contents.html
There is full documentation for the object model here: https://msdn.microsoft.com/en-us/library/wss56bz7.aspx but bear in mind that it is addressed to VBA programmers.
If you want to stream real time data into Excel from Python, you can use an RTD function. If you've ever used the Bloomberg add-in use for accessing real time market data in Excel then you'll be familiar with RTD functions.
The easiest way to write an RTD function for Excel in Python is to use PyXLL. You can read how to do it in the docs here: https://www.pyxll.com/docs/userguide/rtd.html
There's also a blog post showing how to stream live tweets into Excel using Python here: https://www.pyxll.com/blog/a-real-time-twitter-feed-in-excel/
If you wanted to write an RTD server to run outside of Excel you have to register it as a COM server. The pywin32 package includes an example that shows how to do that, however it only works for Excel prior to 2007. For 2007 and later versions you will need this code https://github.com/pyxll/exceltypes to make that example work (see the modified example from pywin32 in exceltypes/demos in that repo).
You can't change an Excel file that's being used by another application because the file format does not support concurrent access.

How do I send VBA output to a Python script?

Here is a code snippet:
' Send the query in the body to Jira, and get the response from Jira
strJsonResponse = Utilities.JiraRequest(strJQLQuery)
I'd like to put this json into a Python parser and then return the parsed json back into the spreadsheet. Is this possible? I've never worked with VBA before.
I have downloaded the JIRA API module and the openpyxl module.
You might be able to create a new text file and output VBA to that
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Fileout As Object
Set Fileout = fso.CreateTextFile("C:\your_path\vba.txt", True, True)
Fileout.Write strJsonResponse
Fileout.Close
VBA code found here
then have VBA start up your python script where you can have python parse your file. There is probably a better way to do this but I'm not sure what.
I think that Tehscript raised a valid question. Are you certain that there's a need to use VBA code in addition to your Python code? By running Python code that makes use of the openpyxl module you can read out and modify basically everything that's in an Excel file, completely without the need to run an Excel application.
Sometimes there are valid reasons for coupling VBA and Python code. The most common one is when you want to use Excel as a familiar graphical user interface to direct data manipulation operations, but want Python to do the actual heavy-lifting. If you're looking to do this, then there's the xlwings module. Xlwings makes it easy to call Python code from inside VBA modules, and to exchange data values between VBA and Python.

How to run excel formulas from python script?

I have an excel spreadsheet with some complicated calculations implemented.
Is there a way I can feed the spreadsheet with input data, run the calculations and get the results from the python script level?
http://www.pythonexcels.com/2009/10/python-excel-mini-cookbook/
http://oreilly.com/catalog/pythonwin32/chapter/ch12.html
Driving Excel from Python in Windows
..doable using the win32 python API

Accessing Vlookup Macro Code in Excel?

I wish to manually code VLOOKUP in python, so is it possible to see the VBA code behind VLOOKUP?
I remember a presentation in my school by a guest speaker showing that the Excel Functions are just macro/vba codes. Can someone please show me the way to view the code for Excel Worksheet functions?
I do not know of a way to view the XL code for VLOOKUP (its almost certainly written in C). The basic algorithms used in VLOOKUP are Linear Search for unsorted data, and Binary Search for sorted data. There are many examples available on the web of implementing these algorithms.
Excel functions i dont' know, but to Excel Add-ins it's possible.
Add a add-in to your excel application, and go to Visual Basic Editor, double click in add-in and use this passwords.
Add-in -- Password
Internet Assistant VBA -- Weezaarde!?
Autosave Addin -- Wildebeest!!
Analysis Toolpak VBA -- Wildebeest!!
Solver Add-in -- Wildebeest!!
All forms be design in excel forms sheets used in older versions of Excel.
[]'s

Categories

Resources