2

簡単な質問: Python-pptx モジュールを使用して検索と置換オプション ( Ctrl+ ) を使用するにはどうすればよいですか?H

コード例:

from pptx import Presentation

nameOfFile = "NewPowerPoint.pptx" #Replace this with: path name on your computer + name of the new file.

def open_PowerPoint_Presentation(oldFileName, newFileName):
    prs = Presentation(oldFileName)

    prs.save(newFileName)
open_PowerPoint_Presentation('Template.pptx', nameOfFile)

「Template.pptx」という名前の Power Point ドキュメントがあります。私の Python プログラムでは、いくつかのスライドを追加して、いくつかの写真を入れています。すべての写真がドキュメントに配置されると、別のパワー ポイント プレゼンテーションとして保存されます。

問題は、この "Template.pptx" に "Week 20" のように古い週番号がすべて含まれていることです。Pythonにこれらすべての単語の組み合わせを見つけて「週25」に置き換えさせたい(たとえば)。

4

7 に答える 7

1

各図形の各スライドにアクセスし、使用可能なテキスト機能を使用して一致を探す必要があります。PowerPoint にはランが奇妙に見える部分に分割される傾向があるため、見栄えがよくない可能性があります。スペルチェックなどの機能をサポートするためにこれを行いますが、そこでの動作は予測できません。

そのため、Shape.text などで出現箇所を見つけるのはおそらく簡単です。状況の詳細によっては、フォントの書式設定を失うことなくそれらを置き換えることは、より困難になる場合があります。

于 2016-06-21T07:00:00.737 に答える
0

フォーマットされたプレースホルダーが複数の実行オブジェクトに広がるという同様の問題が発生しました。フォーマットを残したいので、段落レベルでの差し替えができませんでした。最後に、プレースホルダーを置き換える方法を見つけました。

variable_pattern = re.compile("{{(\w+)}}")
def process_shape_with_text(shape, variable_pattern):
if not shape.has_text_frame:
    return

whole_paragraph = shape.text
matches = variable_pattern.findall(whole_paragraph)
if len(matches) == 0:
    return

is_found = False
for paragraph in shape.text_frame.paragraphs:
    for run in paragraph.runs:
        matches = variable_pattern.findall(run.text)
        if len(matches) == 0:
            continue
        replace_variable_with(run, data, matches)
        is_found = True

if not is_found:
    print("Not found the matched variables in the run segment but in the paragraph, target -> %s" % whole_paragraph)

    matches = variable_pattern.finditer(whole_paragraph)
    space_prefix = re.match("^\s+", whole_paragraph)

    match_container = [x for x in matches];
    need_modification = {}
    for i in range(len(match_container)):
        m = match_container[i]
        path_recorder = space_prefix.group(0)

        (start_0, end_0) = m.span(0)
        (start_1, end_1) = m.span(1)

        if (i + 1) > len(match_container) - 1 :
            right = end_0 + 1
        else:
            right = match_container[i + 1].start(0)

        for paragraph in shape.text_frame.paragraphs:
            for run in paragraph.runs:
                segment = run.text
                path_recorder += segment

                if len(path_recorder) >= start_0 + 1 and len(path_recorder) <= right:
                    print("find it")

                    if len(path_recorder) <= start_1:
                        need_modification[run] = run.text.replace('{', '')

                    elif len(path_recorder) <= end_1:
                        need_modification[run] = data[m.group(1)]

                    elif len(path_recorder) <= right:
                        need_modification[run] = run.text.replace('}', '')

                    else:
                        None


    if len(need_modification) > 0:
        for key, value in need_modification.items():
            key.text = value
于 2021-07-09T10:35:20.280 に答える