Python을 사용하여 PowerPoint 편집

Python 개발자는 프로그래밍 방식으로 PowerPoint 프레젠테이션을 쉽게 편집할 수 있습니다. Python을 사용하여 외부 응용 프로그램을 설치하지 않고도 슬라이드 콘텐츠를 업데이트할 수 있습니다. 이 기사는 Python에서 REST API를 사용하여 PowerPoint 프레젠테이션을 편집하는 방법에 중점을 둘 것입니다.

이 문서에서는 다음 항목을 다룹니다.

문서 편집기 REST API 및 Python SDK

PPTX 편집에는 GroupDocs.Editor Cloud의 Python SDK API를 사용하겠습니다. 프로그래밍 방식으로 워드 프로세싱 문서, Excel 시트 또는 기타 지원되는 형식의 문서를 편집할 수 있습니다. 또한 Cloud API용 문서 편집기 제품군으로 .NET, Java, PHP, Ruby, Android 및 Node.js SDK를 제공합니다.

콘솔에서 다음 명령을 사용하여 Python 프로젝트에 GroupDocs.Editor-Cloud를 설치할 수 있습니다.

pip install groupdocs_editor_cloud

단계와 사용 가능한 코드 예제를 시작하기 전에 dashboard에서 클라이언트 ID와 클라이언트 암호를 얻으십시오. ID와 시크릿이 있으면 아래와 같이 코드를 추가합니다.

client_id = "da0c487d-c1c0-45ae-b7bf-43eaf53c5ad5"
client_secret = "479db2b01dcb93a3d4d20efb16dea971"

configuration = groupdocs_editor_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""

Python에서 REST API를 사용하여 PowerPoint 프레젠테이션 편집

아래에 언급된 간단한 단계에 따라 PowerPoint 프레젠테이션을 편집할 수 있습니다.

  1. 업로드 PPTX 파일을 클라우드에
  2. 편집 업로드된 파일
  3. 다운로드업데이트된 파일

문서 업로드

먼저 아래 제공된 코드 예제를 사용하여 PowerPoint 프레젠테이션을 클라우드에 업로드합니다.

# API 인스턴스 생성
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# 샘플 파일 업로드
request = groupdocs_editor_cloud.UploadFileRequest("sample.pptx", "C:\\Files\\sample.pptx", my_storage)
response = file_api.upload_file(request)

결과적으로 PPTX 파일이 Cloud Storage에 업로드되고 대시보드의 파일 섹션에서 사용할 수 있습니다.

Python을 사용하여 PowerPoint 프레젠테이션 편집

프로그래밍 방식으로 PowerPoint 프레젠테이션을 편집하려면 아래에 언급된 단계를 따르십시오.

  • File APIEdit API 인스턴스 생성
  • 입력 파일 경로 제공
  • PresentationLoadOptions 제공
  • Edit API의 Load 메소드로 파일 불러오기
  • File API의 파일 다운로드 방식을 사용하여 HTML 문서 다운로드
  • 다운로드한 HTML 문서 편집
  • 파일 API의 파일 업로드 방법을 사용하여 HTML을 다시 업로드
  • PPTX에 저장할 PresentationSaveOptions 제공
  • Edit API의 저장 방법을 사용하여 HTML을 PPTX에 다시 저장

다음 코드 조각은 REST API를 사용하여 PowerPoint 프레젠테이션 문서를 업데이트하는 방법을 보여줍니다.

# API 초기화
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)

# 편집 가능한 상태로 로드
fileInfo = groupdocs_editor_cloud.FileInfo("sample.pptx")
loadOptions = groupdocs_editor_cloud.PresentationLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
loadOptions.slide_number = 0
loadOptions.show_hidden_slides = True
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions)) 

# HTML 문서 다운로드
htmlFile = fileApi.download_file(groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path))
html = ""     

# HTML 파일 읽기
with open(htmlFile, 'r') as file:
    html = file.read() 

# 텍스트 바꾸기    
html = html.replace("Hello World", "Welcome")

# HTML을 저장소에 다시 업로드
with open(htmlFile, 'w') as file:
    file.write(html)

fileApi.upload_file(groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile))

# HTML을 PPTX에 다시 저장
saveOptions = groupdocs_editor_cloud.PresentationSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "edited.pptx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
saveOptions.password = "password"
saveResult = editApi.save(groupdocs_editor_cloud.SaveRequest(saveOptions))

# 완료
print("Document edited: " + saveResult.path)
Python을 사용하여 PowerPoint 프레젠테이션 편집

Python을 사용하여 PowerPoint 프레젠테이션 편집

업데이트된 파일 다운로드

위의 코드 샘플은 편집된 PowerPoint 프레젠테이션(PPTX) 파일을 클라우드에 저장합니다. 다음 코드 샘플을 사용하여 다운로드할 수 있습니다.

# API 초기화
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# 파일을 다운로드
request = groupdocs_editor_cloud.DownloadFileRequest("edited.pptx", my_storage)
response = file_api.download_file(request)

# 다운로드한 파일을 작업 디렉토리로 이동
shutil.move(response, "C:\\Files\\")

Python을 사용하여 PowerPoint 프레젠테이션의 이미지 업데이트

프로그래밍 방식으로 PowerPoint 프레젠테이션의 이미지를 업데이트하려면 아래에 언급된 단계를 따르십시오.

  • File APIEdit API 인스턴스 생성
  • 입력 파일 경로 제공
  • PresentationLoadOptions 제공
  • Edit API의 Load 메서드로 파일 불러오기
  • File API의 파일 다운로드 방식을 이용하여 HTML 문서 다운로드
  • 이미지 파일 업로드
  • 다운로드한 HTML 문서 편집 and update the image
  • 파일 API의 파일 업로드 방법을 사용하여 HTML을 다시 업로드
  • PPTX에 저장할 PresentationSaveOptions 제공
  • Edit API의 저장 방법을 사용하여 HTML을 PPTX에 다시 저장

다음 코드 조각은 REST API를 사용하여 PowerPoint 프레젠테이션 슬라이드의 이미지를 업데이트하는 방법을 보여줍니다.

# API 초기화
editApi = groupdocs_editor_cloud.EditApi.from_keys(client_id, client_secret)
fileApi = groupdocs_editor_cloud.FileApi.from_keys(client_id, client_secret)

# 편집 가능한 상태로 로드
fileInfo = groupdocs_editor_cloud.FileInfo("sample.pptx")
loadOptions = groupdocs_editor_cloud.PresentationLoadOptions()
loadOptions.file_info = fileInfo
loadOptions.output_path = "output"
loadOptions.slide_number = 0
loadResult = editApi.load(groupdocs_editor_cloud.LoadRequest(loadOptions)) 

# HTML 문서 다운로드
htmlFile = fileApi.download_file(groupdocs_editor_cloud.DownloadFileRequest(loadResult.html_path))
html = ""     

# HTML 파일 읽기
with open(htmlFile, 'r') as file:
    html = file.read() 

# 교체할 이미지 업로드
request = groupdocs_editor_cloud.UploadFileRequest(loadOptions.output_path + "/sample.files/groupdocs.png", "C:\\Files\\groupdocs.png", "")
response = fileApi.upload_file(request)

# 이미지 바꾸기   
html = html.replace("Picture 2.png", "groupdocs.png")

# HTML을 저장소에 다시 업로드
with open(htmlFile, 'w') as file:
    file.write(html)

fileApi.upload_file(groupdocs_editor_cloud.UploadFileRequest(loadResult.html_path, htmlFile))

# HTML을 PPTX에 다시 저장
saveOptions = groupdocs_editor_cloud.PresentationSaveOptions()
saveOptions.file_info = fileInfo
saveOptions.output_path = "edited.pptx"
saveOptions.html_path = loadResult.html_path
saveOptions.resources_path = loadResult.resources_path
saveResult = editApi.save(groupdocs_editor_cloud.SaveRequest(saveOptions))
PowerPoint 프레젠테이션 슬라이드의 이미지 업데이트

PowerPoint 프레젠테이션 슬라이드의 이미지 업데이트

API는 정의된 PresentationLoadOptions.output\path에 HTML 파일을 생성합니다. 생성된 HTML 파일과 관련된 모든 리소스 파일은 이 경우 “sample.files"와 같은 입력 파일 이름이 접두어로 붙은 파일 하위 디렉토리에 배치됩니다. 이 디렉터리에 이미지를 업로드한 다음 대상 이미지로 교체해야 합니다. 슬라이드의 모든 이미지는 “src” 속성에서 그림 2, 그림 3 등으로 이름이 지정됩니다.

온라인 시도

위의 API를 사용하여 개발된 다음 무료 온라인 PowerPoint 편집 도구를 사용해 보십시오. https://products.groupdocs.app/editor/pptx

결론

이 기사에서는 Python을 사용하여 문서 편집기 REST API를 사용하여 클라우드에서 PowerPoint 프레젠테이션을 편집하는 방법을 배웠습니다. 프로그래밍 방식으로 PPTX 파일을 클라우드에 업로드한 다음 클라우드에서 업데이트된 파일을 다운로드하는 방법도 배웠습니다. 문서를 사용하여 GroupDocs.Editor Cloud API에 대해 자세히 알아볼 수 있습니다. 또한 브라우저를 통해 직접 API를 시각화하고 상호 작용할 수 있는 API 참조 섹션을 제공합니다. 모호한 점이 있으면 언제든지 포럼에 문의해 주십시오.

또한보십시오