Need Jython output in the form of Excel - excel

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.

Related

Changing a variable in .XLSM files using Python, Run Macro and Save it into another file

I am not sure how to develop a functional code. I am using a free macro-enabled software found online and tried to automate this. I found several codes but I still cannot run this properly/the python cannot read the code.
data_location=".xlsm"
output_location=".xlsm"
ori_wb=openpyxl.load_workbook(data_location, keep_vba=True)
ori_sh=ori_wb['Input']
ori_sh[range(E6)]='30' #Changing (e6) value to 30 for example
run.ori_sh
ori_wb.save(output_location)
new_wb=openpyxl.load_workbook(output_location, data_only=True)
new_sh=new_wb.get_sheet_by_name('Input')
print(new_sh)
enter image description here

Open .exe file using python and trying to pass parameter in the same python script

I'm trying to open a .exe file from Python and give it some instructions. Because I have thousands of models to run I need to automatize the process.
Here on Stackoverflow, I found several options that I tried. I am able to open .exe file but not able to fill the information in that and run the .exe. the place is always empty. I'm writing one of these solutions. [I'm using Python3]:
import os
your_bat_file_address = r'"C:\D_drive\testing\SAR\1100_star3\exmple.bat"' # example
os.startfile(your_bat_file_address)
in the exmple.bat file
"C:/D_drive/tool/EXMPLE.exe" --input1 "C:/D_drive/file/1st_file" --input2 "C:/D_drive/file/2st_file" --input3 "C:/D_drive/file/3st_file"

Writing into a Jupyter Notebook from Python

Is it possible for a Python script to write into a iPython Notebook?
with open("my_notebook.ipynb", "w") as jup:
jup.write("print(\"Hello there!\")")
If there's some package for doing so, can I also control the way cells are split in the notebook?
I'm designing a software tool (that carries out some optimization) to prepare an iPython notebook that can be run on some server performing scientific computations.
I understand that a related solution is to output to a Python script and load it within a iPython Notebook using %load my_python_script.py. However, that involves a user to type stuff that I would ideally like to avoid.
Look at the nbformat repo on Github. The reference implementation is shown there.
From their docs
Jupyter (né IPython) notebook files are simple JSON documents, containing text, source code, rich media output, and metadata. Each segment of the document is stored in a cell.
It also sounds like you want to create the notebook programmatically, so you should use the NotebookNode object.
For the code, something like, should get you what you need. new_cell_code should be used if you have code cells versus just plain text cells. Text cells should use the existing markdown formatting.
import nbformat
notebook = nbformat.v4.new_notebook()
text = """Hello There """
notebook['cells'] = [nbformat.v4.new_markdown_cell(text)]
notebook= nbformat.v4.new_notebook()
nbformat.write(notebook,'filename.ipynb')

How do I use python to show data in excel?

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

xlwings : Object Required

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

Resources