0

i am trying to add a table to a presentation using python-pptx with a specific dimensions

i created a slide layout from power-point which contains the table placeholder in the area i want and loaded it using python-pptx.

slide_layout

but regardless of the placeholder dimensions, the table itself after creation is exceeding the placeholder are.

mainly it is dependent on the number of rows as per the documentation "The table's height is determined by the number of rows."

shape_id, name, height = self.shape_id, self.name, Emu(rows * 370840)

i tried to update the placeholder.py file manually and change the row height but the same output appears.

shape_id, name, height = self.shape_id, self.name, Emu(rows * 18429)

the table is insisting on exceeding the placeholder area as per the below image

output

below is my code, any clues ?

from pptx import Presentation
# the presentation including the table placeholder
prs = Presentation('Presentation2.pptx')
slide1 = prs.slides.add_slide(prs.slide_layouts[11])
table_placeholder = slide1.shapes[0]
shape = table_placeholder.insert_table(rows=22,cols=2)
prs.save('test.pptx')

1 Answers1

1

Tables in PowerPoint expand vertically to accommodate new rows; that's just the way they work. You will need to do any resizing yourself, which you may find is a challenging problem. This isn't the same kind of problem when a user is creating a table in the application because they will just make adjustments for fit until it looks best for their purposes.

You'll need to adjust font-size and row-height and perhaps other attributes like column-width etc. based on your application and whatever heuristics you can resolve, perhaps related to the row count and length of text in certain cells and so on.

A table placeholder really just gives a starting position and width.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • i already tried to reduce the row height with a significant change but it didn't affect the table at all – Yasser Arafa Jun 19 '19 at 05:57
  • 1
    Well, I don't see anywhere where you're reducing the font size, and the font-size doesn't "shrink" automatically to fit. You haven't included a picture of your output so I can't say, but if one or more row-cells is "text-bound", like no blank space under it, the row isn't going to get any shorter. – scanny Jun 19 '19 at 17:05