pptx-python: find table height dynamically after adding text - python

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.

Related

QTableView - how to highlight only text with color (not entire cell)

I would like to for some of the columns in my QTableView give color highlight to the text without filling the rest of the cell with color. This image shows exactly what I want:
What is the best way to do this? One option is to create each text value as an image externally and then set it as an icon (I know how to do that from other SO posts). But is there a better way?
I will do this for quite many tables / columns with many different values so I would need to create many images if I use icons. So it would be great if there is a way to instead write the text in the QTableView and somehow apply the color background so that it covers the text only.
Is it possible to e.g. paint that background using an item delegate somehow? Has someone done something similar with item delegates before and that can share an example of how it's done? I'm very new to Qt.
I'm using PyQt6.

Python pptx insert a picture in a table cell

Is there a way to insert a picture inside a cell using pptx python?
I'm also thinking of finding the coordinate of the cell and adjust the numbers for inserting the picture, but can not find anything.
Thank you.
No, unfortunately not. Note that this is not a limitation of python-pptx, it is a limitation of PowerPoint in general. Only text can be placed in a table cell.
There is nothing stopping you from placing a picture shape above (in z-order) a table cell, which will look like the picture is inside. This is a common approach but unfortunately is somewhat brittle. In particular, the row height is not automatically adjusted to "fit" the picture and changes in the content of cells in prior rows can cause lower rows to "move down" and no longer be aligned with the picture. So this approach has some drawbacks.
Another possible approach is to use a picture as the background for a cell (like you might use colored shading or a texture). There is no API support for this in python-pptx and it's not without its own problems, but might be an approach worth considering.

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.

wxPython: how to lay one panel over another

This is about wxPython.
I would like to have 2 Panels laying one over the other:
PanelBG should be some sort of a "background", with its own GridBagSizer with subPanels, StaticTexts and so on;
PanelFG should be the "foreground" panel, also with its own GridBagSizer with some StaticTexts, Buttons... but a transparent background, in such a way that PanelBG is visible wherever PanelFG doesn't lay widgets.
I need both Panels to stretch to all the sides of the frame, even when resizing the window, though never changing the reciprocal proportions, that's why I'm not sure if there's a way to use absolute positioning.
In case you are wondering, the reason why I don't want to use a single Panel is that merging the 2 GridBoxSizers would require me to place many many more cells in the sizer, because rows and columns of foreground and background don't always coincide, and I should split them in many cells, with grid dimensions growing up to hundreds**2.
Since the content I want to put in the foreground needs to be updated and refreshed quite often, this would require redrawing all the cells every time, which would take 10 - 20 seconds to complete the operation (tested). Updating only the foreground would require just some hundredths of a second instead.
Thank you!
This would be at least partially a change of direction, but it might be worth examining what other rendering options you have.
In particular, I'm thinking of wxWebKit (http://wxwebkit.kosoftworks.com/), which would let you do layering, etc. using the WebKit browser rendering engine. I'm not sure whether it's at a stage that would provide everything you need since I haven't actually used it, but even if it doesn't work then it may be an approach worth trying - using HTML/CSS for part of your display, while wrapping the whole in a wxPython app.
As I understand it, this is a calendar with rectangles for the days containing the events for the days.
The simple thing would be to use a wxGrid, with seven columns and four or five rows, to represent the months. You would then place the events into the grid cell for the correct date. The wxGrid widget would look after the details of refreshing everything properly.
Using wxGrid you might lose a little control over the exact appearance, though wxGrid is very flexible and feature rich once you learn its many methods, but you would save yourself having to write large amounts of code that would require significant effort to debug.

Finding out how many lines can be displayed in wx.richtext.RichTextCtrl without scrolling

I'm writing an e-book reader in Python + wxPython, and I'd like to find out how many lines of text can be displayed in a given RichTextCtrl with the current formatting without scrolling.
I thought of using and dividing the control's height by RichTextCtrl.GetFont().GetPixelSize(), but it appears that the pixel size parameter of wx.Font is only specified on Windows and GTK. In addition, this won't cover any additional vertical spacing between lines/paragraphs.
I could of course get the font size in points, attempt to get the display's resolution in ppi, and do it that way, but 1) the line spacing problem still remains and 2) this is far too low a level of abstraction for something like this.
Is there a sane way of doing this?
EDIT: The objective is, to divide the ebook up into pages, so the scrolling unit is a whole page, as opposed to a line.
Source code of PageDown method suggest that there is not a sane way to do this...
Here is my insane proposition (which breaks widget content, caret, displayed position...) which scroll one page and measure how long this scroll is...
def GetLineHeight(rtc):
tallString = "\n".join([str(i) for i in xrange(200)])
rtc.SetValue(tallString)
rtc.SetInsertionPoint(0)
rtc.PageDown()
pos = rtc.GetInsertionPoint()
end = tallString.find("\n",pos)
lineHeight=int(tallString[pos:end])
return lineHeight
Did you try calling the GetNumberOfLines() method? According to Robin Dunn, that should work, although it doesn't take wrapped lines into account.

Categories

Resources