Python 개발자는 프로그래밍 방식으로 Word 또는 Excel 문서를 편집해야 할 수 있습니다. 외부 응용 프로그램을 설치하지 않고도 이러한 문서를 업데이트할 수 있습니다. 이 기사에서는 REST API를 사용하여 Word 또는 Excel 문서를 편집하는 방법에 중점을 둘 것입니다.

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

문서 편집기 REST API 및 Python SDK

GroupDocs.Editor 클라우드 제품군

Word 문서나 Excel 시트를 편집하려면 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

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

client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"

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

REST API를 사용하여 Word 문서 편집

아래에 언급된 간단한 단계에 따라 Word 문서를 편집할 수 있습니다.

문서 업로드

먼저 아래 제공된 코드 예제를 사용하여 Word 문서를 클라우드에 업로드합니다.

storage_api = groupdocs_editor_cloud.StorageApi.from_config(config_info)
file_api = groupdocs_editor_cloud.FileApi.from_config(config_info)

# 샘플 파일 업로드
files = glob.glob("C:\\Files\\sample_word_document.docx", recursive=False)
destination_file = files[0].replace("C:\\Files\\", "", 1)
file_api.upload_file(groupdocs_editor_cloud.UploadFileRequest(destination_file, files[0]))

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

Python에서 Word 문서 편집

프로그래밍 방식으로 Word 문서를 편집하려면 아래에 언급된 단계를 따르십시오.

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

다음 코드 스니펫은 REST API를 사용하여 Word 문서를 업데이트하는 방법을 보여줍니다.

# 필요한 API 인스턴스 생성
edit_api = groupdocs_editor_cloud.EditApi.from_config(configurations)
file_api = groupdocs_editor_cloud.FileApi.from_config(configuration)

# 문서가 이미 저장소에 업로드되었습니다.
# 편집 가능한 상태로 로드
file_info = groupdocs_editor_cloud.FileInfo("sample_word_document.docx", None, None, None)
load_options = groupdocs_editor_cloud.WordProcessingLoadOptions()
load_options.file_info = file_info
load_options.output_path = "output"
load_result = edit_api.load(groupdocs_editor_cloud.LoadRequest(load_options))

# HTML 문서 다운로드
html_file = file_api.download_file(groupdocs_editor_cloud.DownloadFileRequest(load_result.html_path))
html = ""
with open(html_file, 'r') as file:
    html = file.read()

# 수정...
html = html.replace("Sample test text", "Hello world")

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

file_api.upload_file(groupdocs_editor_cloud.UploadFileRequest(load_result.html_path, html_file))

# html을 다시 docx에 저장
save_options = groupdocs_editor_cloud.WordProcessingSaveOptions()
save_options.file_info = file_info
save_options.output_path = "output/edited.docx"
save_options.html_path = load_result.html_path
save_options.resources_path = load_result.resources_path
save_result = edit_api.save(groupdocs_editor_cloud.SaveRequest(save_options))

# 완료
print("Document edited: " + save_result.path)

업데이트된 파일 다운로드

위의 코드 샘플은 편집된 Word 파일을 클라우드에 저장합니다. 다음 코드 샘플을 사용하여 다운로드할 수 있습니다.

request = groupdocs_editor_cloud.DownloadFileRequest("output\\edited.docx", my_storage)
response = file_api.download_file(request)

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

REST API를 사용하여 Excel 시트 편집

프로그래밍 방식으로 Excel 시트를 편집하려면 아래에 언급된 단계를 따르십시오.

  • File APIEdit API 인스턴스 생성
  • SpreadsheetLoadOptions 제공
  • Edit API의 Load 메소드로 파일 불러오기
  • File API의 파일 다운로드 방식을 이용하여 HTML 문서 다운로드
  • HTML 문서 편집
  • File API의 파일 업로드 방법을 사용하여 HTML을 다시 업로드
  • XLSX에 저장할 SpreadsheetSaveOptions 제공
  • Edit API의 Save 메서드를 사용하여 HTML을 다시 XLSX에 저장

아래 제공된 간단한 코드 예제는 REST API를 사용하여 Excel 시트를 업데이트하는 방법을 보여줍니다. 앞에서 언급한 단계에 따라 파일을 업로드하고 다운로드하십시오.

# 필요한 API 인스턴스 생성
edit_api = groupdocs_editor_cloud.EditApi.from_config(configurations)
file_api = groupdocs_editor_cloud.FileApi.from_config(configurations)

# 문서가 이미 저장소에 업로드되었습니다.
# 편집 가능한 상태로 로드
file_info = groupdocs_editor_cloud.FileInfo("sample_four_sheets.xlsx")
load_options = groupdocs_editor_cloud.SpreadsheetLoadOptions()
load_options.file_info = file_info
load_options.output_path = "output"
load_options.worksheet_index = 0
load_result = edit_api.load(groupdocs_editor_cloud.LoadRequest(load_options))

# HTML 문서 다운로드
html_file = file_api.download_file(groupdocs_editor_cloud.DownloadFileRequest(load_result.html_path))
html = ""
with open(html_file, 'r') as file:
    html = file.read()

# 수정...
html = html.replace("This is sample sheet", "This is sample sheep")

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

file_api.upload_file(groupdocs_editor_cloud.UploadFileRequest(load_result.html_path, html_file))

# html을 다시 xlsx에 저장
save_options = groupdocs_editor_cloud.SpreadsheetSaveOptions()
save_options.file_info = file_info
save_options.output_path = "output/edited.xlsx"
save_options.html_path = load_result.html_path
save_options.resources_path = load_result.resources_path
save_result = edit_api.save(groupdocs_editor_cloud.SaveRequest(save_options))

# 완료
print("Excel sheet edited: " + save_result.path)

결론

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

또한보십시오