python-pptx 访问表格单元格属性并在替换期间将其应用回来

问题描述 投票:0回答:2

我正在尝试使用下面的代码

python-pptx
替换powerpoint中的表格标题

slides = [slide for slide in ip_ppt.slides]
shapes = []
for slide in slides:
    for shape in slide.shapes:
        shapes.append(shape)

def replace_text(replacements:dict,shapes:list):
            if shape.has_table:
                for row in shape.table.rows:
                    for cell in row.cells:
                        if match in cell.text:
                            new_text = cell.text.replace(str(match), str(replacement))
                            cell.text = new_text


replace_text({'FY2122':'FY22/23'},shapes)
replace_text({'FY2021':'FY21/22'},shapes)
replace_text({'FY1920':'FY20/21'},shapes)

虽然替换成功,但是替换后的术语丢失了字体大小、字体颜色和对齐方式等格式。例如下面的例子

当我按

cell.f
选项卡时,它仅填充
for
find
。当我按
cell.c
选项卡时,它只会填充
curr_text
cell
cells
。我本来期待看到
color
,但它没有出现。

所以,我尝试了以下方法,但他们返回了

None

            for cell in row.cells:
                for paragraph in cell.text_frame.paragraphs:
                    print(paragraph.font.size)
                    print(paragraph.font.color)
                    print(paragraph.font.alignment)

除了值替换之外,我还想在 for 循环中显式编码,以将相同的格式(例如字体大小、字体颜色等)分配/复制到我的替换术语。

我想提取表格中单元格的属性(字体颜色、字体大小等)并将其分配回同一单元格(替换后)

python automation formatting powerpoint python-pptx
2个回答
2
投票

为了他人的利益回答我自己的问题。我深入到运行级别以提取名称、大小等字体属性,并将其重新分配回我的替换文本。这解决了我的问题

   if shape.has_table:
        for row in shape.table.rows:
            for cell in row.cells:
                paras = cell.text_frame.paragraphs
                for para in paras:
                    #print(para.text)
                    for run in para.runs:
                        if match in run.text:
                            new_text = run.text.replace(str(match), str(replacement))
                            fn = run.font.name
                            fz = run.font.size 
                            run.text = new_text
                            run.font.name = fn
                            run.font.size = fz

0
投票

在我的仓库上下载 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")

演示

© www.soinside.com 2019 - 2024. All rights reserved.