이 문서는 Python에서 Word 문서의 속성을 변경하는 방법에 대한 지침을 제공합니다. 응용 프로그램 개발을 위한 환경을 설정하는 모든 요소와 Python에서 Word 메타데이터를 변경하기 위해 실행할 수 있는 샘플 코드와 함께 따라야 할 단계 목록이 있습니다. 또한 요구 사항에 따라 인덱스 또는 속성 이름을 사용하여 선택한 속성에 액세스하는 옵션에 대해 알아봅니다.
Python에서 Word 메타데이터를 편집하는 단계
- .NET을 통한 Python용 Aspose.Words을(를) 사용하여 메타데이터를 업데이트하도록 IDE를 설정합니다.
- Document 개체를 사용하여 소스 파일을 로드하고 사용자 지정 속성 컬렉션에 액세스
- 대상 속성이 있는지 확인한 다음 속성에 액세스하고 새 값을 설정하십시오.
- built-in properties에 액세스하고 각각의 값을 업데이트합니다.
- 결과 Word 파일을 새 속성으로 저장
이러한 단계는 Python에서 Word의 문서 속성을 편집하는 프로세스를 캡슐화합니다. 프로세스는 소스 문서를 로드하고 항목 인덱스를 사용하여 개별 속성에 액세스하는 custom_document_properties 컬렉션을 사용하여 사용자 지정 속성에 액세스하는 것으로 시작됩니다. 마찬가지로 내장 속성은 built_in_document_properties 컬렉션을 사용하여 액세스하고 수정합니다.
Python에서 Word 문서 메타데이터를 편집하는 코드
import aspose.words as aw | |
import aspose.pydrawing as drawing | |
from datetime import datetime, date | |
# Load the license | |
wordLic = aw.License() | |
wordLic.set_license("Aspose.Total.lic") | |
# Load the original document | |
doc = aw.Document("SampleProps.doc") | |
# Get custom properties | |
custProps = doc.custom_document_properties | |
if custProps.__getitem__(custProps.index_of("Authorized")).value != None: | |
# Set properties | |
custProps.__getitem__(custProps.index_of("Authorized By")).value = "John" | |
custProps.__getitem__(custProps.index_of("Authorized Date")).value = date(2023, 6, 12) | |
custProps.__getitem__(custProps.index_of("Authorized Revision")).value = 200 | |
custProps.__getitem__(custProps.index_of("Authorized Amount")).value = 400 | |
# Get built-in properties | |
documentProperties = doc.built_in_document_properties | |
# Set new properties | |
documentProperties.__getitem__(documentProperties.index_of("Subject")).value = "Test Subject" | |
documentProperties.__getitem__(documentProperties.index_of("Manager")).value = "Test Manager" | |
documentProperties.__getitem__(documentProperties.index_of("Company")).value = "Test Company" | |
# Save the output | |
doc.save("Output.doc"); | |
print ("Word file metadata is updated") |
이 코드 세그먼트는 Word 메타데이터 변경자를 개발하는 과정을 보여줍니다. getitem() 메서드에는 액세스할 속성의 인덱스가 필요합니다. 이를 위해 index_of() 메서드는 속성의 이름이 필요하지만 대상 속성의 인덱스를 알고 있으면 getitem() 메서드를 사용합니다.
이 기사에서는 메타데이터를 변경하는 프로세스에 대해 자세히 살펴보았습니다. Word 파일에 책갈피를 삽입하는 과정을 알아보려면 Python을 사용하여 Word에 책갈피를 삽입하는 방법의 문서를 참조하세요.