简体   繁体   English

如何使用 pandas 和 python-pptx 修改 powerpoint 中的现有表?

[英]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] ).我已经有了 dataframe(3 列,8 行)并且可以访问表格(例如slide[0].shape[1] )。 How can I use this dataframe to update the data (movies, tickets sold, % watched?)如何使用此 dataframe 更新数据(电影、售票、观看百分比?)

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. PowerPoint 表格中的每个单元格都是一个文本容器,API 中没有“从序列(矩阵)序列填充此表格的单元格”方法。

Note that all values must be type str (or unicode if you're on Python 2).请注意,所有值都必须是str类型(如果您使用的是 Python 2,则必须是unicode类型)。 A PowerPoint table has no notion of numbers or formulas like Excel does. PowerPoint 表格没有 Excel 那样的数字或公式概念。 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.没有什么能阻止您创建自己的函数,例如上面的populate_table(matrix, table)并在适合您的任何地方使用它。

download pptx-tables @ https://github.com/bharath5673/python-pptx-tables下载 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演示在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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