0

We need to generate a report using a template. Some slides contain tables, where the data will be pulled from data sources and write it to the table cells, finally save the report with a new name. While writing to table cells, we need to write it dynamically, means if there is a table overflow, duplicate the slide, continue adding data in the next slide.

Consider the following scenario.

  1. Read a ppt template
  2. Read a table in a slide, say the row height of the cell we are on is x Pt.
  3. I replace some text on the cell, now the height of the row becomes x+y Pt.
  4. Now, when I find that row's height, I still get x Pt.

is there any way I can dynamically get the row size as I insert data in table cells ??? so that I can calculate table height and render the remaining table in the next slide.

Note: I am able to duplicate the slide, able to write to the same table in the next slide through a code hack from this library.

I have tried myself with following approach.

  1. Whenever I add some text to cell, calling this function with the presentation object
  2. Save it to temp file
  3. Read the slide and table with given id
  4. Read the row height and return it
def save_file_get_cell_height(presentation_obj, slide_id, table_shape_id, row_idx):
    """
    This function saves given presentation object and re-reads it, returns given table row height
    """
    new_ppt_name = "temp.pptx"
    presentation_obj.save(new_ppt_name)
    new_pr_obj = AbstractPresentation(new_ppt_name)
    slide_obj = new_pr_obj.presentation.slides[slide_id]
    table_obj = None
    for shape in slide_obj.shapes:
        if shape.shape_id == table_shape_id:
            table_obj = shape.table
            break
    assert table_obj, "Invalid table shape id passed"
    new_cell_height = AbstractPresentation.get_cell_height(table_obj, row_idx)
    os.remove(new_ppt_name)
    return new_cell_height

Then, I am surprised that it's still be the same. Then, I opened PPT manually and resized the row height manually with touchpad, then executed above code, then found that it's giving new height.

I dig more deeper,

  1. created a blank PPT in powerpoint.
  2. Added a simple table with few rows and entered some big data.
  3. unzip <ppt_file> then, opened slide.xml file, saw the height, but height of all rows are same.
  4. Then, manually resized row height.
  5. Again unzipped file, then found that height value has changed.

It's neither an issue from python-pptx side nor from microsoft, but I want to get cell height dynamically ??? How could I do that ???

Currently, I am thinking of approximating height based on parameters like font size, cell width, height and number of characters in the text etc... I feel that's not so accurate, so looking for better solution...

Lokesh Sanapalli
  • 1,012
  • 3
  • 18
  • 39

0 Answers0