Creating excel chart from text file - python

I am running a simulation that saves some result in a file from which I take the data I need and save it in
result.dat
like so:
SAN.statisticsCollector.Network Response Time
Min: 0.210169
Max: 8781.55
average: 346.966666667
I do all this using python, and it was easy to convert result.dat into an excel file using xlwt. The problem is that creating charts using xlwt is not possible. I than came across Jpype, but installation on my ubuntu 12.04 machine was a headache. I'm probably just being lazy but still - is there any other way, not necessarily python-related, to convert result.dat into an excel file with charts?
Thanks
P.s the file I want to create is a spreadsheet, not Microsoft's Excel!

there is a now a new possibility: http://pythonhosted.org/openpyxl/charts.html
and http://xlsxwriter.readthedocs.org/chart.html

The main problem is that currently there's no Python library that implements MS Excel chart creation, and, obviously, they will not appear due to lack of good chart format documentation (as python-excel.org guys told) and its huge complecity.
There are two other options though:
Another option is to use 3-rd party tools (like JPype that you've mentioned) combining them with Python scripts. As far as I know, except Java smartXML there's no libraries that are capable of creating excel charts (or course, there are ones for .NET, e.g. expertXLS) and I'm not sure it will run on Mono + IronPython, though you can try.
The third option is Win32 COM API, e.g. like described in this SO post, which is not quite an option for you due to your working operating system.

Related

Import model from 3DSlicer into Unity automatically

For my bachelor thesis I have to convert DICOM data to a 3d model and import this into unity so I can create a virtual reality. So the data out of 3DSlicer has to be updated in Unity automatically. You can say I have to link the two programs. What do you think is the best way to do this? Should I create a Matlab or Python module or is there a better way?
I am sorry if there is already a similar question but I have searched for answers on the internet and unfortunately I couldn't find an answer.
I would really appreciate your help.
Since slicer is compatible to python codes. you can try to access to the unity codes, if you use ubuntu it is probably easier to use a shell script to call the unity functions you need withing the python codes of slicer.
I did it with ANSYS softwares to link two different softwares together and export an output from the first software and pass it to the second software.
Rather than this automatic way, this is a guideline about how to export data from other apps to Unity
https://docs.unity3d.com/Manual/HOWTO-exportFBX.html

Do I need to have Excel installed to use Openpyxl "interactively"? Can it interact with .ODS format?

I have currently written a Python script that uses UNO to interact with a LibreOffice .ods document. However, it is very unstable and is causing a lot of system errors and crashing on Ubuntu 17.04.
I read through the openpyxl docs but I can't find the answer to this quick question: Can I use openpyxl on Ubuntu to dynamically manipulate a .XLSX or a .ODS document with embedded formulas?
I.e. I want to open the document, read data from the cells, update the cell values in a loop, read the new output data from the document's own formulas, and then load this output into a numpy array and close the document without saving it.
Does Excel/libreoffice have to be installed for openpyxl to use the embedded formulas in this way?
Thank you
OpenPyXL doesn't need and cannot use a running instance of Excel. It doesn't interact with .ods at all. The closest thing to OpenPyXL for .ods is pyexcel-ods. The only practical way to evaluate Excel formulas is with a running instance of Excel. Likewise, the only practical way to evaluate LibreOffice formulas is with a running instance of LibreOffice.
So if you want to use OpenPyXL or pyexcel-ods "interactively", you would have to not use Excel/LibreOffice formulas but instead do all the calculations with Python.
If you need the formulas and you need to run it in Linux, then using UNO in some form (to control a running instance of LibreOffice) is probably still your best bet. Note that there are a few different Python interfaces to UNO, such as PyOO and UnoTools. I don't know what you're using now.
Finally, if the UNO-based methods are really too unstable, and you do have access to Excel, then you could try xlwings. It would be fairly convoluted to get this to run in Linux (it might require a virtual machine; testimonials for Excel under Wine have been mixed at best).

Access to LibreOffice's Compare Documents using python

I have two .csv files that I want to use LibreOffice's compare documents tool (Edit>Compare Document) with.
These csv files are made after the run of a long and involved script, and it would be nice to be able to have the compare process to be automatic as well, with the result being a window of LibreOffice open with the changes as if I selected compare manually. I want the specific LibreOffice gui (which I believe does a great job highlighting differences) not just a diff.
Looking online, it seems like there is nice but limited set of python wrappers for libre office (pyoo).
However, despite related questions, I couldn't see any way of gaining access to the compare functionality through this or any other library. Is the Compare Documents functionality available at the python level, the UNO API level, or simply not available at all?
Use the dispatcher:
Dispatcher.executeDispatch(
(XDispatchProvider)Frame, ".uno:CompareDocuments", "", 0, propertyValueFile);
A complete Java example is at https://forum.openoffice.org/en/forum/viewtopic.php?f=44&t=2795.

Manipulate Excel from Python

I would like to manipulate an Excel spreadsheet from Python, e.g. open it, and execute an embedded VBA macro, or some set of actions on the data inside. I have a choice of whether I would like to work on Windows or Linux for this.
There are packages ways to do this (xlwings package from this answer) but I am looking for something more native to explore what options are there.
I think one way to do this is the COM interface on Windows, which xlwings itself wraps. I found win32com python module, but seem to have very little documentation on manipulating particular objects, and what methods and attributes are available. Can someone please point me to a good information source (books are also ok)?
Alternatively, are there other options to do this, and if so, where can I read up on them?
I have gotten around this by using the Auto_Open() function in VBA, which runs when the Excel file opens.
Public Sub Auto_Open()
''' Run other macro here
End Sub
You could then open the Excel file using subprocess.popen. This is a bit of a hack, but avoids using win32com. I had to take this route because win32com wouldn't work on our locked down computers.

How to open Excel instance in python on MAC?

I think this question has been asked before but it's not clear, in the original question the user has provided excel.exe which is a windows executable extension and not for mac.
I need to open new Excel instance in Python on MAC.
which module should I import?
I'm a newbie I have completed learning python language, but have trouble understanding documentation.
If all you need to do is launch Excel, the best way to do it is to use LaunchServices to do it.
If you have PyObjC (which you do if you're using the Python that Apple pre-installs on 10.6 and later; otherwise, you may have to install it):
import Foundation
ws = Foundation.NSWorkspace.sharedWorkspace()
ws.launchApplication_('Microsoft Excel')
If not, you can always use the open tool:
import subprocess
subprocess.check_call(['open', '-a', 'Microsoft Excel'])
Either way, you're effectively launching Excel the same way as if the user double-clicked the app icon in Finder.
If you want to make Excel do something simple like open a specific document, that's not much harder. Look at the NSWorkspace or open documentation to see how to do whatever you want.
If you actually want to control Excel—e.g., open a document, make some changes, and save it—you'll want to use its AppleScript interface.
Apple's recommended way of doing that is via ScriptingBridge, or using a dual-language approach (write AppleScripts and execute them via NSAppleScript—which, in Python, you do through PyObjC). However, I'd probably use appscript (get the code from here). Despite the fact that it's been abandoned by its original creator, and is only being sparsely maintained, and will probably eventually stop working with some future OS X version, it's still much better than the official solutions.
Here's a sample (untested, because I don't have Excel here):
import appscript
excel = appscript.app('Microsoft Excel')
excel.workbooks[1].column[2].row[2].formula.set('=A2+1')
From the comments it is not completely clear if you need to 'update' an Excel file with data, and just assume that you need Excel to do so, or that you need to change some excel files to include new data.
It is usually much easier, and certainly faster (wrt excution speed) to go with 'updating' an Excel file without starting Excel. However updating is not the right word: you have to read in the file and write it out new. You can of course overwrite the orginal file, so it looks like an update.
For 'updating' you can use the trio xlrd, xlwt, xlutils if the files you work with are .xls files (Excel 2003). IIRC xlwt does not support .xlsx for writing (but xlrd can read those files).
For .xlsx files I use openpyxl,
Both are good enough for writing things like data, formula and basic formatting.
If you have existing Excel files which you use as 'templates' with information that would get lost if you read/write using one of the above packages, then you have to go with updating the file in Excel. I had to do so because I had no easy way to include Visual Basic macros and very specific formatting specified by a client. And sometimes it is just easier to visually setup a spreadsheet and then just fill the cells programmatically. But this was all done on Windows.
If you really have to drive Excel on Mac, because you need to use existing files as templates, I suggest you look at Applescript. Or, if it is an option, look at OpenOffice/LibreOffice PyUno interface.

Categories

Resources