import win32com.client import os # Your specific file path pst_path = r'd:\Dropbox\!!!Days\Downloads Z230\PST\tkulhava.pst' def main(): if not os.path.exists(pst_path): print(f"Error: File not found at {pst_path}") return try: # Connect to Outlook outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") # 1. Add the PST to Outlook (This makes it visible in the sidebar) print(f"Mounting PST: {pst_path}...") outlook.AddStore(pst_path) # 2. Find the folder object for this PST # We search specifically for the folder that matches the filename 'tkulhava' # or grab the last added store if the name doesn't match exactly. pst_name = "tkulhava" # derived from filename usually root_folder = None # Loop through all stores to find the new one for folder in outlook.Folders: if pst_name.lower() in folder.Name.lower(): root_folder = folder break # Fallback: Just grab the last folder in the list if name didn't match if not root_folder: root_folder = outlook.Folders.GetLast() print(f"Successfully opened root folder: {root_folder.Name}") print("=" * 50) # 3. Start the recursive walk print_subjects_recursively(root_folder) # 4. Cleanup: Remove the PST from Outlook # (Comment this out if you want to keep it open in Outlook to inspect manually) outlook.RemoveStore(root_folder) print("\nDone. PST detached.") except Exception as e: print(f"An error occurred: {e}") def print_subjects_recursively(folder): """ Recursively prints subjects of emails in a folder and its subfolders. """ try: # Print current folder name for context # Check if folder has items if folder.Items.Count > 0: print(f"\n--- Folder: {folder.Name} ---") # Iterate through items for item in folder.Items: try: # Class 43 is a standard MailItem. # Other items (meeting requests, reports) might not have a Subject or behave differently. if item.Class == 43: print(f"Subject: {item.Subject}") else: # Attempt to print subject anyway (e.g., for Meeting Items) print(f"[{type_name(item.Class)}] Subject: {item.Subject}") except Exception: # Skip items that are corrupted or unreadable pass # Recursion: Go deeper into subfolders for subfolder in folder.Folders: print_subjects_recursively(subfolder) except Exception as e: print(f"Skipping restricted folder '{folder.Name}': {e}") def type_name(class_id): # Helper to identify non-email items if class_id == 53: return "Meeting" if class_id == 46: return "Report" return f"Type {class_id}" if __name__ == "__main__": main()