Python에서 PST 파일 생성 및 정리
새 PST 파일 생성 및 하위 폴더 추가
Aspose.Email는 처음부터 Personal Storage Table (PST) 파일을 생성하고 하위 폴더를 추가하는 기능을 제공합니다. With the PersonalStorage 클래스를 사용하면 이메일 메시지, 캘린더 이벤트, 연락처 및 기타 데이터를 저장하는 PST 파일과 관련된 작업을 관리할 수 있습니다.
다음 코드 샘플은 새 저장 파일을 생성하고 그 안에 "Inbox" 폴더를 추가하는 방법을 보여줍니다:
- 다음 사용 PersonalStorage.create 지정된 디렉터리로 새 PST 파일을 생성하는 메서드. 파일은 the 를 사용하여 생성됩니다
UNICODE현대 애플리케이션과의 호환성을 위한 포맷. - PST 파일의 루트 폴더에 접근하고 "Inbox"라는 새 하위 폴더를 추가하여 이메일 메시지를 정리합니다.
컨테이너 클래스 매칭 검사
Aspose.Email for Python은 PST 파일에서 폴더 생성 시 검증 프로세스를 강화하는 솔루션을 제공합니다. enforce_container_class_matching 속성을 사용하여 the FolderCreationOptions 클래스를 사용하면 PST 저장소에 새 폴더를 추가할 때 컨테이너 클래스가 엄격히 일치하도록 할 수 있습니다. 이 기능은 컨테이너 클래스 불일치를 방지하여 PST 파일 내 조직 계층을 유지하는 데 도움을 줍니다. ‘EnforceContainerClassMatching’을 ’true’로 설정하면 부모와 자식 폴더의 컨테이너 클래스가 일치하지 않을 경우 예외가 발생해 잘못된 폴더 구조를 방지합니다. 이 속성의 기본값은 ‘false’이며, 폴더 생성 시 유연성을 허용하면서 필요할 때 엄격한 컨테이너 클래스 일치를 강제할 수 있습니다.
다음 코드 샘플은 enforce_container_class_matching 속성을 사용하여 컨테이너 클래스가 일치하지 않는 폴더를 추가할 때 예외를 발생시킬지 여부를 제어하는 방법을 보여줍니다:
with PersonalStorage.create("storage.pst", FileFormatVersion.Unicode) as pst:
contacts = pst.createpredefinedfolder("Contacts", StandardIpmFolder.Contacts)
# An exception will not arise. EnforceContainerClassMatching is False by default.
contacts.addsubfolder("Subfolder1", "IPF.Note")
# An exception will occur as the container class of the subfolder being added (IPF.Note)
# does not match the container class of the parent folder (IPF.Contact).
contacts.addsubfolder("Subfolder3", FolderCreationOptions(enforcecontainerclassmatching=True, containerclass="IPF.Note"))
폴더 컨테이너 클래스 변경
때때로 폴더 클래스를 변경해야 할 필요가 있습니다. 일반적인 예는 서로 다른 유형(약속, 메시지 등)의 메시지가 동일한 폴더에 추가될 때입니다. 이 경우 폴더 내 모든 요소가 올바르게 표시되도록 폴더 클래스를 변경해야 합니다. 아래 코드 스니펫은 이를 위해 PST에서 폴더의 컨테이너 클래스를 변경하는 방법을 보여줍니다:
성능 향상을 위한 일괄 메시지 추가
PST 파일에 개별적으로 추가하는 대신 대량으로 메시지를 추가하면 효율성과 성능 측면에서 여러 이점이 있습니다: I/O 작업 감소, 작업 완료 시간 단축, 시스템 자원 효율적 사용 등. add_messages 메서드는 the FolderInfo 클래스는 소스 폴더에서 가져온 MAPI 메시지 컬렉션을 대상 폴더로 전달하는 데 사용됩니다.
다른 PST에서 메시지 추가
PST 파일의 소스 폴더에서 모든 MAPI 메시지를 열거하고 가져오려면, 의 enumerate_mapi_messages() 메서드를 사용합니다 FolderInfo 클래스. 그런 다음, 이 메시지들을 다른 PST 파일의 대상 폴더에 추가합니다.
import aspose.email as ae
src_pst = ae.storage.pst.PersonalStorage.from_file("source.pst", False)
dest_pst = ae.storage.pst.PersonalStorage.from_file("destination.pst")
# Get the folder by name
src_folder = src_pst.root_folder.get_sub_folder("SomeFolder")
dest_folder = dest_pst.root_folder.get_sub_folder("SomeFolder")
dest_folder.add_messages(src_folder.enumerate_mapi_messages())
디렉터리에서 메시지 추가
디렉터리에서 메시지를 추가하려면 파일을 열고 PST 파일 내 특정 폴더에 대한 참조를 얻은 후, "path" 변수로 지정된 디렉터리에서 파일 이름 목록을 가져와 각 파일을 MapiMessage로 로드하기 위한 빈 MSG 리스트를 생성합니다. 각 로드된 메시지를 msg_list에 추가합니다. 아래 코드 샘플은 디렉터리에서 메시지를 추가하는 과정을 보여줍니다:
import aspose.email as ae
import os
pst = ae.storage.pst.PersonalStorage.from_file("my.pst", False)
# Get the folder by name
folder = pst.root_folder.get_sub_folder("SomeFolder")
dirs = os.listdir("path")
msg_list = []
for file in dirs:
msg = ae.mapi.MapiMessage.load(file)
msg_list.append(msg)
folder.add_messages(iter(msg_list))