简体   繁体   中英

How to modify existing tables in powerpoint using pandas and python-pptx?

I have a table similar to this that I would like to modify: 桌子

I'm still new to this library and I can't seem to understand how to modify existing tables from the documentation. What's the best way to do it? I already have the dataframe (3 columns, 8 rows) and I can access the table (eg slide[0].shape[1] ). How can I use this dataframe to update the data (movies, tickets sold, % watched?)

You'll have to write a short routine to do this cell by cell. Each cell in a PowerPoint table is a text-container and there's no "fill the cells of this table from a sequence of sequences (matrix)" method in the API.

Note that all values must be type str (or unicode if you're on Python 2). A PowerPoint table has no notion of numbers or formulas like Excel does. Each cell contains only text.

Something like this would do the trick in simple cases, but you can see already that there can be complexities like "what if I want to skip a header line" or "what if the matrix is a different size than the table", or "what if I want to format numbers with particular decimal places depending on the column", etc.

M = {"matrix", some sequence of sequences like a `numpy` array or list of lists}
table = {"table" object retrieved from a shape on a slide}
populate_table(table, M)

def populate_table(table, M):
    for row in range(len(M)):
        row_cells = table.rows[row].cells
        for col in range(len(M[0]):
            row_cells[col].text = str(M[row][col])

The code involved is small and easily customizeable and the complexity of designing and then using a general-purpose solution is fairly high, so this is left up to the user to work out for themselves.

There's nothing stopping you from creating a function of your own like the populate_table(matrix, table) above and using it wherever it suits you.

download pptx-tables @ https://github.com/bharath5673/python-pptx-tables

from pptx import Presentation
from pptx.util import Inches, Pt
from pptx_tables import PptxTable

prs=Presentation()
prs.slide_width = Inches(13.333)
prs.slide_height = Inches(7.5)


data = [
        [['Aa', 'Bb','Cc'],
        [3,     4,      5],
        [6,     7,      8]],

        [['Dd', 'Ee','Ff'],
        [0,     1,      2],
        [6,     7,      8]]
        ]



for n,lst in enumerate(data):

    lyt=prs.slide_layouts[6] # choosing a slide layout
    slide=prs.slides.add_slide(lyt) # adding a slide
    # title=slide.shapes.title 
    title_name = f"Page : "+str(n)
    # title.text=title_name
    # subtitle=slide.placeholders[1]


    tbl = PptxTable(lst,prs)
    tbl.set_table_location(left=Inches(1.5), top=Inches(2), width=Inches(5))
    # tbl.set_formatting(font_size=Pt(7), row_height=Inches(.3),alignment=PP_PARAGRAPH_ALIGNMENT.LEFT)
    tbl.set_formatting(font_size=Pt(12), row_height=Inches(.85))        
    tbl.create_table(slide_index=n,
                      columns_widths_weight=[2, 2, 2],
                      transpose=True,
                      )


tbl.save_pptx("slide_table.pptx")

demo在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM