Skip to content

Instantly share code, notes, and snippets.

@pixelchai
Created August 29, 2020 00:36
Show Gist options
  • Save pixelchai/cc807a7116d30152cfe917cb776e6108 to your computer and use it in GitHub Desktop.
Save pixelchai/cc807a7116d30152cfe917cb776e6108 to your computer and use it in GitHub Desktop.
Quick script I made to automate exporting all my emails from my Outlook inbox as .msg files and renaming them all to be the timestamp of the message
import os
import time
import shutil
import extract_msg
import dateparser
import datetime
import pygetwindow as gw
import pyautogui as gui
import traceback
import ctypes
OUT_PATH = "out"
BUF_PATH = "buf"
OLDEST = datetime.datetime(2020, 8, 21).timestamp()
TEST = False
if TEST:
import sys
file_path = os.path.join(BUF_PATH, os.listdir(BUF_PATH)[0])
msg = extract_msg.Message(file_path)
print(msg.date)
msg.close()
sys.exit()
w_outlook = gw.getWindowsWithTitle(' - Outlook')[0]
screen_w, screen_h = gui.size()
n = 0
while True:
w_outlook.activate() # focus Outlook window
time.sleep(0.1)
gui.hotkey("ctrl", "c") # ctrl+c
time.sleep(0.2)
gui.press("up") # up
# focus explorer window
gui.moveTo(screen_w*0.75, screen_h*0.75)
# for some reason gui.click() sometimes fails with FileNotFoundError so workaround:
ctypes.windll.user32.mouse_event(2, 0, 0, 0,0) # left down
ctypes.windll.user32.mouse_event(4, 0, 0, 0,0) # left up
time.sleep(0.1)
gui.hotkey("ctrl", "v") # ctrl + v
# poll buf folder
files = []
print("Waiting", end="", flush=True)
while len(files) <= 0:
files = os.listdir(BUF_PATH)
print(",", end="", flush=True)
time.sleep(0.2)
break_flag = False
for file in files: # should only be one but loop just in case
file_path = os.path.join(BUF_PATH, file)
# keep trying to read file (while still being written to)
d = None
while d is None:
try:
msg = extract_msg.Message(file_path)
d = dateparser.parse(msg.date)
except:
pass
print(".", end="", flush=True)
time.sleep(0.2)
msg.close()
del msg # free up resource
if d.timestamp() < OLDEST:
timestamp = "{:010.0f}".format(d.timestamp()) + "{:03d}".format(n%1000)
# n append to timestamp: crude system to avoid duplicate filenames if many emails received within same second (up to 1000 collisions handled)
# move file (keep retrying until successful)
while True:
print("!", end="", flush=True)
try:
shutil.move(file_path, os.path.join(OUT_PATH, timestamp + ".msg"))
break
except PermissionError:
traceback.print_exc()
pass
time.sleep(0.2)
print("") # newline
print("Done #{:04d}: {}".format(n, d.strftime("%c")))
n += 1
time.sleep(0.1)
else:
break_flag = True
break
if break_flag:
break
print("Fin!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment