scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.28k stars 503 forks source link

Is there a way to create shapes from scratch using xml? #856

Open buenohernandez opened 1 year ago

buenohernandez commented 1 year ago

Or for example, setting table.element.xml = xml_specimen (This doesn't work)

MartinPacker commented 1 year ago

Isn't that because there's more to python-pptx's object model than chunks of XML?

To the OP's point, why XML rather than the calls python-pptx offers? I'm trying to understand the requirement.

MartinPacker commented 1 year ago

It starts with a call like this:

        newTableShape = slide.shapes.add_table(rows, columns, 0, 0, 0, 0)

(from my md2pptx project - which makes tables from Markdown).

buenohernandez commented 1 year ago

It is terrible using this on mobile, so easy to misclick.

So, let's say you have pandas generated dataframes, and they have different styling, that maybe can be converted into xml. How would automate a crration of several tables on a slides using that exact styling (color, stripes, aligment) but having different shapes? And one of the requirements of this solution is it being generic, in other words hard coding a new template everytime it appears is out of question.

Did I help you understand?

Since you are here, I kindly accept suggestions.

MartinPacker commented 1 year ago

I think most of the table attributes you could want are supported by python-pptx. (Not being the developer I can't claim all could be.)

When you create tables, rows, cells - which python-pptx certainly supports - you would have to parse the attributes from the input and apply them. And cells contain collections of runs - which can be individually styled.

(Note: My md2pptx doesn't pretend to copy attributes - other than alignment and width - as it's generating tables from Markdown.)

buenohernandez commented 1 year ago

I already have a full functioning table_from_df, and it outputs a quite pretty table, my issue is that the template is hard coded and everytime a new template is needed I will have to recode it.

That is why I was looking for a "one size fits all" solution.

You gave me an idea, I can have the wanted table template made by hand (by the user) as a placheholder and my code just inserts the values 🤔.

Thank you!

And I will look into your code, there's probably more insights for me there.

MartinPacker commented 1 year ago

That would work. It is easy to find the table on the slide in code.

I took the view most users of md2pptx would be leaning on their template presentation's styling - with the occasional override. Alignment and column widths are the obvious ones. But borders are another, subtler one. As is stripey rows.

buenohernandez commented 1 year ago

It didn't work, I got stuck on obtaining properties like, bold, size, and font name, they all return None. It should be easy but its not that simple

https://stackoverflow.com/questions/56853071/run-font-italic-and-run-font-bold-return-none-instead-of-true