Reputation: 63
I have a table similar to this that I would like to modify:
I'm still new to this library and I can't seem to understand how to modify existing tables from the documentation. What's the best way to do it? I already have the dataframe (3 columns, 8 rows) and I can access the table (e.g. slide[0].shape[1]
). How can I use this dataframe to update the data (movies, tickets sold, % watched?)
Upvotes: 2
Views: 3322
Reputation: 28873
You'll have to write a short routine to do this cell by cell. Each cell in a PowerPoint table is a text-container and there's no "fill the cells of this table from a sequence of sequences (matrix)" method in the API.
Note that all values must be type str
(or unicode
if you're on Python 2). A PowerPoint table has no notion of numbers or formulas like Excel does. Each cell contains only text.
Something like this would do the trick in simple cases, but you can see already that there can be complexities like "what if I want to skip a header line" or "what if the matrix is a different size than the table", or "what if I want to format numbers with particular decimal places depending on the column", etc.
M = {"matrix", some sequence of sequences like a `numpy` array or list of lists}
table = {"table" object retrieved from a shape on a slide}
populate_table(table, M)
def populate_table(table, M):
for row in range(len(M)):
row_cells = table.rows[row].cells
for col in range(len(M[0]):
row_cells[col].text = str(M[row][col])
The code involved is small and easily customizeable and the complexity of designing and then using a general-purpose solution is fairly high, so this is left up to the user to work out for themselves.
There's nothing stopping you from creating a function of your own like the populate_table(matrix, table)
above and using it wherever it suits you.
Upvotes: 3