Reputation: 163
I want to create a table to be as short as it can possibly be, i.e. have the smallest row height possible, while fitting the prescribed text at the prescribed size. I am using this code:
I initialize my table:
init_table_width = prs.slide_width - Inches(0.2)
graphicframe = slide.shapes.add_table(rows = len(biosets) + 1, cols=5, left=Inches(0.1), top=Inches(.76), width=init_table_width, height=prs.slide_height - Inches(.76 + 3.2))
table = graphicframe.table
I then populate the table and set cell properties like this:
for i in range(len(biosets)):
new_row = ['Bioset'+str(i+1)+': '+biosets[i], studies[i], correlation[i], common_genes[i], pvals[i]]
rownum += 1
for col_index, col_name in enumerate(new_row):
cell = table.cell(rownum, col_index)
cell.text = col_name
cell.margin_left = cell.margin_right = cell.margin_top = cell.margin_bottom = 0;
cell.text_frame.paragraphs[0].font.size = Pt(7.5)
When I open the ppt presentation, the table is taller than I’d like. I can drag the bottom of the table up and reduce the height of the table from that which was automatically created. Why is there extra height that can be reduced, if I set margins to 0? The table that was created has a height GREATER than the height I initialized for the table; when I manually drag & reduce table height, the height is LESS than the height I initialized for the table. This means it COULD have fit all my contents into a table of the size I desired. Text did wrap in a few rows, so that may explain why it expanded from its original size. Even so, why is there some extra height that I can drag and reduce if I already set cell margins to 0? What element is contributing this and how can I get rid of it? I don’t want to hard code row.height, because I don’t know when text will wrap on to two lines.
Upvotes: 3
Views: 4391
Reputation: 163
I figured out that if I initialize the table to an impossibly small height (see height=1 below), the table that gets created will have as compact a row height as possible.
graphicframe = slide.shapes.add_table(len(biosets) + 1, cols=5, left=Inches(0.1), top=Inches(.76), width=init_table_width, height=1)
Upvotes: 3