I'm a newbie into both python and xlwings. So the story is,
I need a custom function needs to be used in Excel. Since I have no clue about VB scripts, I decided to write a function in python and import it in excel using xlwings.
I installed xlwings using the pip command. I added the addin to excel
by the procedure given in xlwings support forum.
I created an excel file, say " Test.xlsm". I created a python file in
the same name "Test.py" (File is in same folder only)
I wrote my function in the python
import xlwings as xl
#xl.func
def exponent(x,y):
#the function is an example only. I tried this for practicing and it is also not working
z=x**y
return z
I opened excel, imported the functions using import function in
xlwings addin. I found no errors in importing the functions
I called the functions from the excel cell,
"=exponent(A1,B1)"
Instead of getting a result, I'm getting "Object Required"
I don't know what went wrong?
Any ideas what I'm missing? Forgive me for the basic question.
You need to add the Reference in VBA.
Open up the Developer console (Alt-F11)
Click on Tools -> References and select xlwings
Related
This is the error message i am getting .Even on trying the code given by my tutor and online i tried fixing but ended up being not able to fix the problem
You're requesting Pandas to use the xlsxwriter engine to write your XLSX file. The error says
ModuleNotFoundError: No module named 'xlsxwriter'
To use the xlsxwriter engine, you'll need to install the xlsxwriter module.
Usually, that's done with pip install xlsxwriter.
Windows 10, Python 3.6, xlwings 0.27.8
When trying to debug my code outside of RunPython, I keep stumbling on the following issue, for example:
import xlwings as xw
xlsx_file = 'anExcelFile.xlsm'
xlwx = xw.book(xlsx_file).set_mock_caller()
From there, I am hoping to be able to use xlsw as normally as such if I had used the routine from RunPython, but now, typing xlsw returns None
However, if i do:
xlsx = xw.book(xlsx_file).set_mock_caller
then xlsx contains: <Book ['anExcelFile.xlsm]>
but still, xlsx() returns None.
Any lead on what I am getting wrong would help, thanks!
Finally understood my issue.
As explained in xlwings, the code sequence should read as follows:
import xlwings as xw
xlsx_file = 'anExcelFile.xlsm'
xw.book(xlsx_file).set_mock_caller()
# Sets the Excel file which is used to mock xw.Book.caller()
# when the code is called from Python and not from Excel via RunPython.
# and then:
xlsx = xw.Book.caller()
Now, xlsx returns: <Book [anExcelFile.xlsm]>
I'm trying to import a user-defined function(UDF) via xlwings but am encountering an issue. Upon pressing the import functions button in the xlwings ribbon, I receive the following run time error:
`Run_time error '1004
`Method of 'VBProject' of object '_Workbook' failed.'
According to the VBA debugger, the below module contains the problem:
Sub ImportXlwingsUdfsModule(tf As String)
' Fallback: This is called from Python as direct pywin32 calls were
' sometimes failing, see comments in the Python code
On Error Resume Next
ActiveWorkbook.VBProject.VBComponents.Remove
ActiveWorkbook.VBProject.VBComponents("xlwings_udfs")
On Error GoTo 0
**ActiveWorkbook.VBProject.VBComponents.Import tf**
End Sub
The .py file containing the UDF is saved in the same folder as the calling .xlsm workbook.
How do I rectify this so I can utilize UDFs?
Thanks to the xlwings team for providing a link that helped me resolve the issue.
One needs to ensure that Trust Access to VBA object model is enabled.
See: https://docs.xlwings.org/en/stable/udfs.html#one-time-excel-preparations
If you have trusted access to the VBA object model and you are getting a TypeError, there is an additional answer that applies to both versions 0.22.2 and 0.22.3 and maybe earlier.
In file Lib>Site-Packages>xlwings>udfs.py on line 651 or 652 (depending on the version) insert ".Item" where shown in the following:
xl_workbook.VBProject.VBComponents.Remove(xl_workbook.VBProject.VBComponents.Item("xlwings_udfs"))
I do not know why this works but it does. The original line does work in VBA with only modifications that apply to VBA syntax and the workbook reference. (i.e. see the VBA code in the question for an example.
applist = AdminApp.list().split("\n")
for a in applist:
print a
continue
I need the output of this script in the form of excel sheet
I'm assuming you want to create and write data to an Excel file with Jython. There are several ways to do it:
You can use Java from Jython and use Apache POI - example
You can simply write to a CSV file, and on Windows, Excel should open it automatically.
You can use a Python library like xlwt or openpyxl.
You'll have to install it, e.g.: easy_install openpyxl.
Then you can follow the tutorial here You didn't say if this code runs on your WebSphere instance, if so it may be a bit harder to install a Python module, but surely not impossible.
I'm trying to display data from Python in Excel. Ideally, a pandas dataframe's worth of data would appear in a new, unsaved excel instance. My search has turned up ways to create excel files, and lots of ways to 'open' an excel file to read data from it, but no way to display it. My current approach was to create a file and then figure out how to open it, but I consider that approach second-best.
I found this. Another way would be to export your data to CSV and import it in Excel.
I found you can 'run' files with the OS library. As long as your computer knows what to do with it, you can create the xlsx file with whatever method and then run it to display:
import xlsxwriter
import os
w = xlsxwriter.Workbook(r"C:\Temp\test.xlsx")
s=w.add_worksheet("Sheet1")
s.write(1,3,"7")
w.close()
os.startfile(r"C:\Temp\test.xlsx")
Still not sure if you can work with an unsaved open instance of excel