1

I am using python 2.7 with python pptx

I need a table to fit exact size, It has 2 colums It has unknown number of rows each cell has unknown length of text.

I need the entire table to fit some exact location, and would ideally want the text in each -cell- to fit the exact portion of the table that it is supposed to get but the formula (size/rows/2)

I found here how to adjust the size of the text in each cell, but it is not quite sufficient

I found how to change the table size with .width and .height but this is not working when the text is too long and it's just making the table grow larger.

How can i either put a hard constraint on each cell size, filling it with the text in the best way possible (or the closest solution possible)?

Thanks!

Community
  • 1
  • 1
thebeancounter
  • 4,261
  • 8
  • 61
  • 109

2 Answers2

1

I worked on a similar requirement and this is how I handled a table with random number of possible rows.

  1. Identify the column which has the max characters.In my case, I had just one column with varying content.
  2. Try to get a rough estimate on the number of characters that could fit on one slide.
  3. Come up with logic to break the text if it reaches the max chars and move on to next slide, create a table and fill in the remaining content.
Varun
  • 86
  • 1
  • 1
  • 8
0

Set the initial dimensions of your table to your best approximation in the style described by Varun, and then call ONE of the following

table.cell(x,y).text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE 
table.cell(x,y).text_frame.fit_text() 

on the text frames in your table, to have the font modify itself to fit the frame you've given it. I've found these options to be somewhat buggy, but maybe you'll have better luck! Read more here: https://media.readthedocs.org/pdf/python-pptx/latest/python-pptx.pdf

R-Peys
  • 123
  • 1
  • 9