OpenPyxl Excel - Function to change cell width to fit cell value? - python

I'm writing a little program to create an excel file. I've managed to change style attributes of cells, but I was wondering if there was a function or an attribute that automatically changes cells' width based on the length of the cells' values. Just like when you double click on the column to do it manually.
I found a way to change the width to a specific number, but every column's title has a different length and I would end up having a lot of blank space.
Maybe a function calle autofit or something similar?

Related

Using Python XLWT to set style to a range of cells (without merging or writing)

I want to add bold border around range of cells that includes table header and data (e.g. B2:G75) excluding borders between cells. I know that I can make a bunch of styles for each corner of the table and apply them while writing data (checking first/last row and first/last item in row), but I assume this is a bad practice. write_merge won't help me either since it will merge cells.
Below you can find an example of what I want.
Is there a way to apply style to a range of cells using xlwt without merging or writing? Or should I switch to a different Excel library?

pptx-python: find table height dynamically after adding text

I want to find the height of the table dynamically, so that whenever the table's height reaches a specific number, I can create another slide and split the table. Because number of text in cell varies, the height will be different for each time the code is run.
Is it possible to do it in pptx-python?
No. "Stretched" table cell heights and the resulting overall table height turns out to be a rendering concern and those values are not recorded in the .pptx file.
A row or cell can have a minimum height that is set in the file, but once the cell gets more text than it can display it will automatically grow beyond that minimum and the PowerPoint renderer is the one to do that. It does not record the value it ends up with.
One way to approximate the rendered dimensions of a table cell is to render the the text yourself in some sort of graphical environment and see how big it gets. That value will depend on the font type-face and point-size, so you'll need to know those. Any way you go about it will be approximate; there are a lot of small complicating factors and even one renderer like OpenOffice might not always render exactly like another, say PowerPoint or Google Docs. For example, there may also be column width adjustment rules that PowerPoint uses to help accommodate a heavily populated cell, not just making the cell taller.
I've heard of at least one developer having reasonable results using wxPython as a rendering environment in which to approximate PowerPoint's rendering behavior, but I believe that was for a single textbox. Doing the same for a table is potentially much more complex.
Another route might be to use VBA to drive an actually running instance of PowerPoint. That may give access to the rendered size of the table.

How to apply rowheight dynamically in tkinter Treeview

How to set dynamically rowheight in Treeview widget of tkinter?
I mean if I am adding wrapped text into the row then how can I set that particular row's height without affecting other rows' heights. Image 1 is the Treeview object created without selecting the row. When I select the row it cuts out the text I have inserted(Image 2).
Same thing happens if I insert a second row(Image 3), it cuts down to the rowheight I have mentioned in style.
In a recent change what I have done in my program is to check for the length of the string passing to the Treeview. While filling Treeview with the rows set the 3 types of flags for 3 type of heights. Small, Medium and Large are set according to rows passing to the Treeview. Now, I have three flags to check for when I'm done with filling rows of Treeview. Checking from Small to Large, the highest flag set for my case is deciding factor for my all row heights for Treeview so I send according "rowheight" to Treeview.And that's all... I get all rows clear to be read, whenever I apply new changes.
For now this solution is serving for my project. But, still expecting to get individually settable rowheights for rows of Treeview.

Tkinter Treeview - Conditional Formatting - Individual cell colours

I'm currently using Tkinter's Treeview to display a table of data in my GUI, and would like to apply conditional formatting in the Treeview widget so that the background colour of a cell depends on that cell's value (e.g. 0 would be red, and 1-6 are green). Very much like Excel's conditional formatting.
My program reads a CSV file and converts it to a Pandas DataFrame, and I use the Treeview widget to display that data. I also have buttons in my GUI that lets the user filter the data depending on which button is clicked.
I haven't been able to find or think of an easy solution. Would anyone know of a possible work-around or an alternative please?
Desired example output

Text with multiple colors in PsychoPy

I am messing around in PsychoPy now, trying to modify the Sternberg demo for a class project. I want the stimulus text—the number set—to display in a variety of colors: say, one digit is red, the next blue, the next brown, etc. A variety within the same stimulus.
I can only find how to change the color of the entire set. I was wondering if I could add another variable to the spreadsheet accompanying the experiment and have the values in the cells be comma separated (red,blue,brown…). Is this possible?
No, that isn't possible right now. There's an experimental new stimulus class called TextBox that will allow it, but you'd have to write code to use that (not available yet from the Builder interface). Or just create some tif images of your stimuli and use those?
The current way to implement this is to have a separate text stimulus for each digit, each with the desired colour.
If the text representation of the number is contained in a variable called, say, stimulusText then in the Text field for the first text component put "$stimulusText[0]" so that it contains just the first digit. In the next text component , use "$stimulusText[1]", and so on.
The colour of each text component can be either fixed or vary according to separate column variables specified in a conditions file.

Categories

Resources