I want to write a hopefully short python script that would do things with the contents of a specially formatted text cell of an spss data table.
How can I hook on the event that the user clicked into a data cell? How can I then get its value and do things I want? Does spss have clear-cut interface for doing this?
You cannot catch events using the programmability or scripting apis. The only formatting in the Data Editor comes from variable formats (and column width) except for the special coloring using with missing data imputation. Tables in the Viewer, of course, have extensive cell formatting capabilities.
Related
I'm working a lot with Excel xlsx files which I convert using Python 3 into Pandas dataframes, wrangle the data using Pandas and finally write the modified data into xlsx files again.
The files contain also text data which may be formatted. While most modifications (which I have done) have been pretty straight forward, I experience problems when it comes to partly formatted text within a single cell:
Example of cell content: "Medical device whith remote control and a Bluetooth module for communication"
The formatting in the example is bold and italic but may also be a color.
So, I have two questions:
Is there a way of preserving such formatting in xlsx files when importing the file into a Python environment?
Is there a way of creating/modifying such formatting using a specific python library?
So far I have been using Pandas, OpenPyxl, and XlsxWriter but have not succeeded yet. So I shall appreciate your help!
As pointed out below in a comment and the linked question OpenPyxl does not allow for this kind of formatting:
Any other ideas on how to tackle my task?
i have been recently working with openpyxl. Generally if one cell has the same style(font/color), you can get the style from cell.font: cell.font.bmeans bold andcell.font.i means italic, cell.font.color contains color object.
but if the style is different within one cell, this cannot help. only some minor indication on cell.value
Is it possible to write, from SPSS, (using Python), into a newly created Excel file, the variable list and variable labels?
Yes, lookup DISPLAY DICTIONARY and/or CODEBOOK. It would then be a case of exporting these outputs (from SPSS's output viewer) to Excel (OUTPUT EXPORT command).
If you needed something more customized then you can either capture the output via OMS and do manipulations as you please (and then export to Excel) or you can use python APIS directly to retrieve variable, value labels and then write results to Excel (using any Python/Excel library of your choice such as xlrd or xlsxwriter, to name a couple).
The latter requires much more programming knowledge whereas the former can all be done with native SPSS syntax.
I have done something similar (producing a customized data dictionary) taking the Python programming approach and found this module written by an unknown author very useful as a basis.
(Assuming you meant an automated way of achieving this else you could just copy and past the column of variable names and labels to Excel! Value labels can't be done similarly though for obvious reasons).
You can also consult the discussion on
http://www.spssforum.com/viewtopic.php?f=12&t=12076
that also includes some python code (only for the variable labels but value labels are an easy extension, using the GetVariableLabel function. However, it depends a bit on how you want to have them, though. (on separate lines, or as following the variable.)
You may also do it like so, followed by code of e.g. openpyxl:
from savReaderWriter import *
with SavHeaderReader(filename) as header:
report = str(header) # txt report
metadata = header.all()
What solutions exist (and what are the pros/cons) for including free-format, rich text data in an Excel file (along side the normal tabular data)?
This is the kind of data I'd like to include (in a separate worksheet):
Note that we're currently using openpyxl to generate Excel files from Python. We can use something other than Python/OpenPyXL if necessary, but keeping with Excel is a must (the accountants who use these reports won't use anything else).
The Excel specification makes it pretty tricky to do what you want. Currently, the smallest unit that you can apply styles to in openpyxl is a cell. As long as you can work with that restriction, you should be okay. Outlining is also supported.
I would like to know is there any widget like c# (Data Grid View) that displays the data in row and column?
I retrieve data from postgreSQL
The pandas library (http://pandas.pydata.org/), specifically the DataFrame. You can display the table in multiple formats, from HTML (if you're using IPython Notebook, for example) to LaTeX via the functions to_html() or to_latex(), as well as a tabbed printing for working from a console. It does not give you interactive editing within the display, but you can view any portion of the data. It can also read sql directly into a Data Frame.
I have a Python 2.6 app running on Linux that creates a CSV file. From the app, I need to create an HTML report, as a single HTML file, that presents the data from the CSV (probably as a table) and also highlights fields where the values meet certain criteria. Charting type functionality would be a nice to have.
What's the best way to do this?
No GPL stuff please.
Choose a Python csv library from here. Now that you have the data mapped to Python data structures you can iterate on it and create the html. I would use the Jinja2 templating engine which is nicely documented. Highlighting rows/cells would work by setting certain css classes on the respective tr/td elements in the table.