ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python/python-pptx 사용자 가이드 - 4. 프리젠테이션
    코딩/Python 2024. 6. 5. 17:01
    728x90

    https://python-pptx.readthedocs.io/en/latest/user/presentations.html

    Working with Presentations

    python-pptx allows you to create new presentations as well as make changes to existing ones. Actually, it only lets you make changes to existing presentations; it’s just that if you start with a presentation that doesn’t have any slides, it feels at first like you’re creating one from scratch.

    However, a lot of how a presentation looks is determined by the parts that are left when you delete all the slides, specifically the theme, the slide master, and the slide layouts that derive from the master. Let’s walk through it a step at a time using examples, starting with the two things you can do with a presentation, open it and save it.

    python-pptx를 사용하면 새 프레젠테이션을 만들 수 있을 뿐만 아니라 기존 프레젠테이션을 변경할 수도 있다. 실제로 기존 프리젠테이션만 변경할 수 있다. 단지 슬라이드가 없는 프레젠테이션으로 시작하면 처음에는 처음부터 슬라이드를 만드는 것처럼 느껴질 뿐이다.

    그러나 프레젠테이션의 모양은 슬라이드를 모두 삭제할 때 남는 부분, 특히 테마, 슬라이드 마스터, 마스터에서 파생되는 슬라이드 레이아웃에 따라 결정된다. 프레젠테이션으로 할 수 있는 두 가지 작업, 프레젠테이션을 열고 저장하는 것부터 시작하여 예제를 사용하여 한 번에 한 단계씩 살펴본다.

    Opening a presentation

    The simplest way to get started is to open a new presentation without specifying a file to open:

    시작하는 가장 간단한 방법은 열려는 파일을 지정하지 않고 새 프리젠테이션을 여는 것이다.

    from pptx import Presentation
    
    prs = Presentation()
    prs.save('test.pptx')

    This creates a new presentation from the built-in default template and saves it unchanged to a file named ‘test.pptx’. A couple things to note:

    그러면 내장된 기본 템플릿에서 새 프리젠테이션이 생성되어 'test.pptx'라는 파일에 변경되지 않고 저장된다.

    • The so-called “default template” is actually just a PowerPoint file that doesn’t have any slides in it, stored with the installed python-pptx package. It’s the same as what you would get if you created a new presentation from a fresh PowerPoint install, a 4x3 aspect ratio presentation based on the “White” template. Well, except it won’t contain any slides. PowerPoint always adds a blank first slide by default.
    • You don’t need to do anything to it before you save it. If you want to see exactly what that template contains, just look in the ‘test.pptx’ file this creates.
    • We’ve called it a template, but in fact it’s just a regular PowerPoint file with all the slides removed. Actual PowerPoint template files (.potx files) are something a bit different. More on those later maybe, but you won’t need them to work with python-pptx.

    참고할 몇 가지 사항:

    • 소위 "기본 템플릿"은 실제로 슬라이드가 없는 PowerPoint 파일이며, 설치된 python-pptx 패키지와 함께 저장된다. 이는 슬라이드가 포함되어 있지 않다는 점만 빼면 PowerPoint를 새로 설치하여 "흰색" 템플릿을 기반으로 한 4x3 종횡비 프레젠테이션을 새로 만든 경우 얻을 수 있는 것과 동일하다. PowerPoint에서는 기본적으로 항상 빈 첫 번째 슬라이드를 추가한다.
    • 저장하기 전에 아무 작업도 수행할 필요가 없다. 해당 템플릿에 포함된 내용을 정확히 확인하려면 생성된 'test.pptx' 파일을 살펴보라.
    • 우리는 이를 템플릿이라고 부르지만 실제로는 모든 슬라이드가 제거된 일반 PowerPoint 파일일 뿐이다. 실제 PowerPoint 템플릿 파일(.potx 파일)은 약간 다르다. 나중에 더 자세히 설명하겠지만 python-pptx를 사용하는 데는 필요하지 않다.

    REALLY opening a presentation

    Okay, so if you want any control at all to speak of over the final presentation, or if you want to change an existing presentation, you need to open one with a filename:

    최종 프레젠테이션에 대해 어떤 제어 기능을 원하거나 기존 프레젠테이션을 변경하려면 파일 이름을 사용하여 열어야 한다.

    prs = Presentation('existing-prs-file.pptx')
    prs.save('new-file-name.pptx')

    Things to note:

    • You can open any PowerPoint 2007 or later file this way (.ppt files from PowerPoint 2003 and earlier won’t work). While you might not be able to manipulate all the contents yet, whatever is already in there will load and save just fine. The feature set is still being built out, so you can’t add or change things like Notes Pages yet, but if the presentation has them python-pptx is polite enough to leave them alone and smart enough to save them without actually understanding what they are.
    • If you use the same filename to open and save the file, python-pptx will obediently overwrite the original file without a peep. You’ll want to make sure that’s what you intend.

    참고 사항:

    • 이 방법으로 PowerPoint 2007 이상 파일을 열 수 있다. PowerPoint 2003 이하 버전의 .ppt 파일은 작동하지 않는다. 아직 모든 콘텐츠를 조작할 수는 없더라도 이미 포함된 콘텐츠는 모두 로드되고 저장된다. 기능 세트는 아직 구축 중이므로 아직 Notes Pages와 같은 항목을 추가하거나 변경할 수 없다. 하지만 프레젠테이션에 해당 기능이 있는 경우 python-pptx는 해당 기능을 그대로 놔둘 만큼 정중하고 실제로 무엇을 이해하지 않고도 저장할 수 있을 만큼 똑똑하다.
    • 동일한 파일 이름을 사용하여 파일을 열고 저장하면 python-pptx는 삐걱거리는 소리 없이 원래 파일을 순종적으로 덮어쓴다. 그것이 당신이 의도한 것인지 확인하고 싶을 것이다.

    Opening a ‘file-like’ presentation

    python-pptx can open a presentation from a so-called file-like object. It can also save to a file-like object. This can be handy when you want to get the source or target presentation over a network connection or from a database and don’t want to (or aren’t allowed to) fuss with interacting with the file system. In practice this means you can pass an open file or StringIO/BytesIO stream object to open or save a presentation like so:

    python-pptx는 소위 파일류 객체에서 프레젠테이션을 열 수 있다. 파일과 같은 객체에 저장할 수도 있다. 이는 네트워크 연결이나 데이터베이스를 통해 소스 또는 대상 프레젠테이션을 가져오려고 하고 파일 시스템과 상호 작용하는 데 어려움을 겪고 싶지 않거나 허용되지 않는 경우 유용할 수 있다. 실제로 이는 열린 파일이나 StringIO/BytesIO 스트림 객체를 전달하여 다음과 같이 프레젠테이션을 열거나 저장할 수 있음을 의미한다.

    f = open('foobar.pptx')
    prs = Presentation(f)
    f.close()
    
    # or
    
    with open('foobar.pptx') as f:
        source_stream = StringIO(f.read())
    prs = Presentation(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    prs.save(target_stream)

    Okay, so you’ve got a presentation open and are pretty sure you can save it somewhere later. Next step is to get a slide in there …

    프레젠테이션이 열려 있고 나중에 어딘가에 저장할 수 있다고 확신한다. 다음 단계는 거기에 슬라이드를 넣는 것이다.

     

    Python/python-pptx 사용자 가이드

    728x90

    댓글

Designed by Tistory.