[Python] 기존 Powerpoint의 텍스트를 형식을 변경하지 않고 변경 python-pptx
5027 단어 파워 포인트파이썬python-pptx
동작을 확인하기 위해 다음과 같이 포맷 된 문자열이있는 PPT 파일의 일부 ( "...")를 오늘 날짜 (2021/3/6)로 변환하는 코드를 작성해 보겠습니다. 했다. 원래의 문자는 그림자를 붙이거나, 밑줄을 긋거나, 여러가지 장식하고 있습니다. 코드를 실시하시는 분은 "원래 파일.pptx"라고 하는 파일을 만들어 코드와 같은 디렉토리에 보존해 주세요.

덧붙여 기존의 문자를 옮겨놓는 방법은, python-pptx의 작자인 Scanny씨가 여기에서 설명하고 있으므로, 그 함수(모듈)는 그대로 이용합니다.
htps : // 기주 b. 코 m / s 칸 y / py 텐 - ptx / 이스에 s / 285
replace_paragraph_text_retaining_initial_formatting이라는 긴 함수가 그렇습니다.
내가 고안한 포인트는 원래 파일에서 해당 paragraph 객체를 찾는 것입니다. 이 오브젝트의 관계가 여러가지 까다롭습니다만, 이쪽을 참고로 했습니다. 이 계층 중 어느 것이 위였는지, 전혀 기억할 수 없습니다. . .

import pptx, re
from pptx.util import Cm, Pt, Inches
def replace_paragraph_text_retaining_initial_formatting(paragraph, new_text):
p = paragraph._p # the lxml element containing the <a:p> paragraph element
# remove all but the first run
for idx, run in enumerate(paragraph.runs):
if idx == 0:
continue
p.remove(run._r)
paragraph.runs[0].text = new_text
prs = pptx.Presentation("元のファイル.pptx")
slide=prs.slides[0] #先頭のページを選択
for shp in slide.shapes:
if shp.has_text_frame:
if "日付は" in shp.text: #変更する文字列の一部でマッチさせる
new_text=re.sub('。。。', '2021年3月6日', shp.text) #置き換え
replace_paragraph_text_retaining_initial_formatting(shp.text_frame.paragraphs[0], new_text)
prs.save('変更後のファイル.pptx')
Reference
이 문제에 관하여([Python] 기존 Powerpoint의 텍스트를 형식을 변경하지 않고 변경 python-pptx), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Kent-747/items/a60b8638a31549c0ebd3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)