Question

I have to edit a pptx template which contain tables like this:pptx table

How to append values stored in my python dict to the empty fields? I'm using a pptx module, but I couldn't find any example of doing this.

from pptx import Presentation


prs = Presentation('template.pptx')
slide = prs.slides[2] #<- This is the slide that contains the table
shape = slide.shapes #<- As I understood This gives access to the shapes
textframe=shape.textframe
textframe.clear()

prs.save('test.pptx') #<- Saves the new file

pptx module link

Was it helpful?

Solution

quote from the dev-group of python-ppty developer

-if you know its index, something like table = slide.shapes[2] would do the trick. Then you'll need to navigate the cells before you can change their contents:

for idx, row in enumerate(table.rows):
    if idx = 0:  # skip header row
        continue
    name_cell = row.cells[0]
    name_cell.text = 'foobar'
    corners_cell = row.cells[1]

OTHER TIPS


---in main----
table_data = [['ID', 'Name', 'Age', 'Second name'], ['1', 'Petro', 22, 'Petrovich'], ['2', 'Ivan', 32, 'Ivanovich'], ['3', 'Oles', 23, 'Marko']]

prs = Presentation(template_filepath)

slide_1 = slide_build(prs, 5)
table_draw(table_data, slide_1.shapes)
prs.save(result_filepath)


def slide_build(prs, layout):
    slide = prs.slides.add_slide(prs.slide_layouts[layout])
    return slide

def table_draw(table_data, shapes):
    rows_number = 0
    columns_number = 0

    # get table size
    rows_number = len(table_data)
    for i, item in enumerate(table_data):
        columns_number += 1

    table = table_build(rows_number, columns_number, shapes)

    column_coord = 0
    row_coord = 0

    for row_count, row in enumerate(table_data):
        for item_count, row_item in enumerate(row):
            table.cell(row_count + row_coord, item_count + column_coord).text = str(row_item)

def table_build(rows, cols, shapes):
    left = (0.1)
    top = Inches(0.7)
    width = Inches(6.0)
    height = Inches(0.8)
    table = shapes.add_table(rows, cols, left, top, width, height).table

    # set column widths
    i = 0
    while i 

Some thing like this

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top