Python pptx insert a picture in a table cell - python

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.

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.

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.

Remove Text Shapes from Powerpoint slides where the shapes are empty

My md2pptx code creates slides using python-pptx. It sometimes ignores shapes on a page it doesn't need.
In Powerpoint Slide Show these empty shapes don't appear. In LibreOffice they seem to.
I'm pretty adept at manipulating the underlying XML for a slide.
Is it feasible to remove empty shapes - perhaps by deleting their XML elements? Or does python-pptx itself offer the capability to delete a shape? (I think not.)
Assume I can navigate to the shapes and figure out which ones are empty.
Note: I'm not aiming to delete whole slides, just empty shapes.
Deleting a "stand-alone" shape is reliable and pretty easy, something like:
sp = shape._element
sp.getparent().remove(sp)
The problem comes in where the shape has a relationship to some other "package part". For example, a Picture shape has a relationship (identified with an rId) to an image part (file) in the package (.pptx zip archive). In those cases, if you don't also properly deal with the relationship, you may get a "repair error" when you try to open the resulting file in PowerPoint.
A "regular" shape (so-called "auto-shape") such as a rectangle, text-box, line, or other geometric shape has no relationships and can be reliably deleted with this method. A table is probably safe too, but not a chart. A group shape is probably okay too, but only if it does not contain a picture or a chart. Both a picture and a chart may be a problem if you don't also remove their relationship.
Whether or not a repair error is triggered is a behavior that may differ between PowerPoint and LibreOffice (or other PPTX client). You can try just deleting a picture or chart shape without dealing with the relationship and see what happens, but to be reliable you'd need to test it with all the possible clients.
Removing a relationship is a little more involved and is either covered in another python-pptx question here on SO or would make a good new question.

How to define text field in python (Pillow preferred)

I want to define an rectangular area on top of an image, that has got a specific width and a specific height and in which a text should be pasted. I want the text to stay inside of this text field. If the text is longer, its size should be decreased and at one point it should be converted into a multiline text. How can I define such a text field using Pillow in python3?
I am looking for a clean and minimalist solution.
Sadly, I have no idea how to do this, since I am very new to python. I also didn't find any helpful and fitting information on how to do this, in the Internet. Thanks in advance!

Tools to extract glyph data from bitmap font image

I have all the characters of a font rendered in a PNG. I want to use the PNG for texture-mapped font rendering in OpenGL, but I need to extract the glyph information - character position and size, etc.
Are there any tools out there to assist in that process? Most tools I see generate the image file and glyph data together. I haven't found anything to help me extract from an existing image.
I use the gimp as my primary image editor, and I've considered writing a plugin to assist me in the process of identifying the bounding box for each character. I know python but haven't done any gimp plugins before, so that would be a chore. I'm hoping something already exists...
Generally, the way this works is you use a tool to generate the glyph images. That tool will also generate metric information for those glyphs (how big they are, etc). You shouldn't be analyzing the image to find the glyphs; you should have additional information alongside your glyphs that tell where they are, how big they should be, etc.
Consider the letter "i". Depending on the font, there will be some space to the left and right of it. Even if you have a tool that can identify the glyph "i", you would need to know how many pixels of space the font put to the left and right of the glyph. This is pretty much impossible to do accurately for all letters. Not without some very advanced computer vision algorithms. And since the tool that generated those glyphs already knew how much spacing they were supposed to get, you would be better off changing the tool to write the glyph info as well.
You can use PIL to help you automate the process.
Assuming there is at least one row/column of background colour separating lines/characters, you can use the Image.crop method to check each row (and then each column of the row) if it contains only the background colour; thus you get the borders of each character.
Ping if you need further assistance.

Categories

Resources