Adding a bottom line to each row in a table in a PPTX generated by Python

Viewed 2853

I have a table which is generated by, and placed in a PPT file, using the pptx module python-pptx. I want to add a line between each row of the table in my PPT file.

enter image description here

My current code:

prs = Presentation('existing-presentation.pptx')
slide = prs.slides.add_slide(prs.slide_layouts[0])
title = slide.shapes.title
title.text = "Slide Title"
df_to_table(slide, df)
prs.save(output)

def df_to_table(slide, df, colnames=None):

    rows, cols = df.shape
    placeholder = slide.placeholders[10]
    res = placeholder.insert_table(rows+1, cols)    
    res.table.columns[0].width = Pt(50)


    if colnames is None:
        colnames = list(df.columns)

    # Insert the column names
    for col_index, col_name in enumerate(colnames):
        # Column names can be tuples
        if not isinstance(col_name, str):
            col_name = " ".join(col_name)
        res.table.cell(0, col_index).text = col_name
        res.table.cell(0, col_index).text_frame.paragraphs[0].font.size=Pt(8)
        res.table.cell(0, col_index).text_frame.paragraphs[0].font.name='Arial'
        res.table.cell(0, col_index).text_frame.paragraphs[0].font.color.rgb=RGBColor(0, 0, 0)
        res.table.cell(0, col_index).fill.solid()
        res.table.cell(0, col_index).fill.fore_color.rgb=RGBColor(255, 255, 255)

    m = df.as_matrix()

    for row in range(rows):
        for col in range(cols):
            val = m[row, col]
            text = str(val)
            res.table.cell(row + 1, col).text = text
            res.table.cell(row + 1, col).text_frame.paragraphs[0].font.size=Pt(8)
            res.table.cell(row + 1, col).text_frame.paragraphs[0].font.name='Arial'
            res.table.cell(row + 1, col).text_frame.paragraphs[0].font.color.rgb=RGBColor(0, 0, 0)
            res.table.cell(row + 1, col).fill.solid()
            res.table.cell(row + 1, col).fill.fore_color.rgb=RGBColor(255, 255, 255) 
            res.table.rows[row+1].height = Pt(0.5)

            if col == 0: #ignore first col with names
                continue
            else:
                res.table.columns[col].width = Pt(55)
2 Answers
Related