So I know that xlwings can import User Defined Functions to excel from Python and I have no problems when I do that. I also know that xlwings can run macros based on a frozen (executable) python file which removes the requirement for a user to have python installed. My question is if you can merge the two so that User Defined Functions can be run using only a frozen (executable) python file as its source? If so please elaborate on the settings required to do so since I have not successfully done so. Thank you in advance for your time!
Related
I am sorry if this question has already been answered, however it is a topic very little discussed about.
I am trying to run a macro in libreoffice. The macro has been written in python as shown.
import uno, os.path, sys
import pandas
def Bring_from_doc():
doc = XSCRIPTCONTEXT.getDocument()
siz=doc.Sheets
uno, os and sys can be imported without any issue since they are installed in libreoffice python installed folder.
However pandas is not installed and got this error when running script:
This is the directory where libreoffice python libraries are located including uno, os and sys. But pandas and other wanted are not.
My question is: How can be installed pandas and any other required library that can be used by any python script run by libreoffice in a macro?
Thank you!!
On Linux, this is easy. Simply install the library in the system python and it will work from a LibreOffice macro. Verify whether it is python 2 or 3 on your system; for example on Ubuntu, normally I enter python3 as the executable name.
On Windows, this is nearly impossible and I would not recommend it (and have repeatedly stated this on stackoverflow, as it is asked here every so often). Numerous environmental variables must be set correctly and other hacks are required. If you are an expert then it can be done, but since you are asking here, my guess is that you do not have the required skills to make this process go smoothly!
Even if you do get it to work, no one else will be able to use your macro, and you would need to do it all over again if you use a different computer. So I would not recommend it in most cases even for experts.
Alternatives:
Run Linux in a virtual machine.
Install a normal distribution of python elsewhere on your system, and write a normal python script that imports pandas, saving the result for example to an xml file. When that script is finished, run a LibreOffice macro that reads the result file and does not import pandas.
Avoid using specialized libraries at all. This is what I normally do, as the standard python libraries allow you to do quite a bit, maybe not as easily, but you could import some extra code or write workarounds to do what is needed.
I am trying to figure out how to use Python-based functions in excel. I came across Pyxll which can make Python add-ins instead of using VBA. But Pyxll is not free after their 30-day trial.
I also came across xlwings which worked fine and served the purpose of adding udfs in excel, the problem is--- it is not very user-friendly. I have to put the excel file in the same folder as the python file plus, they should also have the same names with different extensions. Or I may use the xlwings quickstart command to do that.
This means I have to create such folders everytime I wish to include my python based functions in a new excel project file and copy paste the functions from the previous python files to the newly created quickstart files.
I was wondering if there is any way to use only one python file to import user-defined functions using xlwings or perhaps a different library/module which is free to use and does that?
(PS: According to the xlwings documentation, we can point to a udf module under the xlwings tab in excel but even after many attempts I am not able to make it work )
I want to create a windows app and send that app to my colleagues for their use, using python. I already wrote a code which imports an excel file and manipulate it to give an output. I used pandas, numpy libraries for that. Is there a way to create a simple standalone windows app indepent of phyton code which imports this excel file and stores in itself, takes some inputs then gives an output? If possible could you recommend me best matched gui libraries for that purpose?
Thanks in advance
I have an excel spreadsheet with a massive amount of VBA and macros that include a button.
How do I execute VBA code in Excel (specifically to clicking the button and trigger its onclick event) outside excel from python (for example)?
Note: I'm willing to take answers in different languages like C++, C#, or Java; but, by far I would prefer them in python since it'll more smoothly connect with the remainder of my python applications.
Note 2: I may need to manipulate the excel spreadsheet with python using one of the python excel libraries available
Version Numbers:
Microsoft Excel Office 365 Version 1708 Build 8431.2079
python 2.7
You can use the win32com library to interact with COM Objects through Python. You can to install the win32com library with pip. That's how I did it at least. Essentially, you are not clicking the button on your worksheet, but instead calling the subroutine embedded in your VBA code to run on your worksheet.
Also, I used Python 3.6 for this, but I believe Python 2.7 should be compatible. Here is a basic outline.
Install Win32Com
Open up and command prompt and type:
pip install pypiwin32
Code
import win32com.client as win32
excel = win32.Dispatch("Excel.Application") # create an instance of Excel
#excel.Visible = 1 #Uncomment this to show Excel working through the code
book = excel.Workbooks.Open(Filename=r'C:\YourBookHere.xlsm')
excel.Run("YourBookHere.xlsm!Sheet1.MacroName") # This runs the macro that is on Sheet1
book.Save()
book.Close()
excel.Quit()
Hope this helps, this is my first post.
Edit: Cleaned up things a bit
To expand upon dylan's excellent answer: you can also accomplish the same using the python xlwings package. In essence xlwings is a fancy wrapper around pywin32 that allows control of an excel application with even simpler syntax. The following python code should do the same as the code from dylan's answer:
import xlwings as xw
# Connect to existing workbook containing VBA macro
wb = xw.Book(r'C:\YourBookHere.xlsm')
# Run the VBA macro named 'MacroName'
wb.macro('MacroName')()
In addition, you can also use Windows Task Scheduler to fire the event. If a button-click-event fires the code, you can do exactly the same thing, by following the instructions in the link below.
https://www.sevenforums.com/tutorials/11949-elevated-program-shortcut-without-uac-prompt-create.html
Also, you will need a Workbook-open-event, so, something like this...
Private Sub Workbook_Open()
Msgbox "Welcome to ANALYSIS TABS"
End Sub
I am trying to integrate xlwings with an excel spreadsheet that we use on our network drive. However, I do not want to install Python, Xlwings, or any other modules on any computer except for the developers. In addition, I would like to use UDFs only, we do not have an interest in using Macros. I read here (https://www.reddit.com/r/Python/comments/22i4a1/xlwings_the_easiest_way_to_deploy_your_python/) that there is a way to freeze your python script which would prevent the need for all users to have Python installed. As a result, I used cx_Freeze to freeze my script. Assume I have these files in the "I" drive and that there are no other files: test.xlsm, test.py, test.exe, Dir (folder containing dll and pyd files generated by cx_Freeze. I am using Python 2.7 and xlwings 0.7.2. Any help would be greatly appreciated!