0

I have inserted a table into a pptx file and I want to auto adjust the column width so that none of the texts in the column cells needs to break line. So simply I am searching for an autoadjust of the whole column.

What I have so far is:

from pptx import Presentation
from pptx.util import Inches
from PandasToPowerpoint import df_to_table
import pandas as pd

d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
     'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)

top = Inches(2.5)
left = Inches(0.48)
width = Inches(12.3)
height = Inches(3.0) 
df_to_table(slide, df.reset_index(), left, top, width, height)


for shape in slide.shapes:
    if shape.has_table:
        table = shape.table
        for column in table.columns: 
            column.width=Inches(3)

prs.save('test.pptx')   

I tried:

column.text_frame.auto_size = True
column.auto_size = True
column.fit_text= True

but they all do not work. If autoadjust is not working I could also iterate through all cells of a column and identify the one with the longest text and adjust the whole column width to this text. But I could also not find the right command to get the text width... Any idea is appreciated!

horseshoe
  • 1,437
  • 14
  • 42
  • Your question has been answered here: https://stackoverflow.com/questions/40626741/python-pptx-determine-a-table-width-and-height – Felix Jost Feb 28 '22 at 08:49

1 Answers1

1

I know you were looking for a solution to adjust the column-width, but how about adjusting the row-height:

import pptx.enum.text MSO_AUTO_SIZE

table.cell(row, column).text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT
ChrisK
  • 11
  • 2