captain shai captain shai - 1 year ago 171
Python Question

python pptx determine a table width and height

I use python 2.7 with python pptx.

I have a table of data and need it to feet certain width and height,

I learned that i can change the text size like so enter link description here

But i wish to change the entire table size to fit an exact location and with changing the font size, and manipulating the row heights and col width it all seems too complex and hard to handle,

I found that i can turn the table to an image and than easily change it's size but that does not feel like the right thing to do.


Answer Source

The example taken from the pptx documentation gives a good example on how to create a table with a given overall width and height as follows:

from pptx import Presentation
from pptx.util import Inches

prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes

shapes.title.text = 'Adding a Table'

rows = cols = 2
left = top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)

table = shapes.add_table(rows, cols, left, top, width, height).table

# set column widths
table.columns[0].width = Inches(2.0)
table.columns[1].width = Inches(4.0)

# write column headings
table.cell(0, 0).text = 'Foo'
table.cell(0, 1).text = 'Bar'

# write body cells
table.cell(1, 0).text = 'Baz'
table.cell(1, 1).text = 'Qux''test.pptx')

The util Module also provides alternatives to specifying the dimensions such as Centipoints, Cm, Emu, Mm, Pt and Px.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download