-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_extract.py
108 lines (90 loc) · 2.31 KB
/
mail_extract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import win32com.client as win32
import datetime
import pandas as pd
outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(
6
) # "6" refers to the index of a folder - in this case,
# the inbox. You can change that number to reference
# any other folder
messages = inbox.Items
sender = []
dates = []
weeks = []
years = []
cats = []
mails = []
for msg in messages:
try:
mail = msg.SenderEmailAddress
except:
mail = "unknow"
mails.append(mail)
try:
SenderName = msg.SenderName
except:
SenderName = "unknow"
sender.append(SenderName)
try:
date = msg.CreationTime
week = date.isocalendar()[1]
year = date.isocalendar()[0]
except:
date = "unknow"
week = "unknow"
year = "unknow"
try:
cat = msg.Sensitivity
except:
cat = "unknow"
cats.append(cat)
dates.append(date)
weeks.append(week)
years.append(year)
df = pd.DataFrame(
{
"sender": sender,
"mail": mails,
"cat": cats,
"date": dates,
"week": weeks,
"year": years,
}
)
df["date"] = df["date"].dt.tz_convert(None)
from openpyxl import load_workbook
import os
cwd = os.getcwd()
path = cwd + "\\mail.xlsx"
excelBook = load_workbook(path)
with pd.ExcelWriter(path) as writer:
writer.book = excelBook
writer.sheets = dict((ws.title, ws) for ws in excelBook.worksheets)
df.to_excel(writer, "extract", index=False)
writer.save()
# # sender=msg.SenderEmailAdress
# # date=msg.CreationTime
# # body_content = msg.Body
# # subject = msg.Subject
# # categories = msg.Categories
# # print(body_content)
# # print(subject)
# # print(categories)
# message = messages.GetLast()
# # body_content = message.Body
# subject = message.Subject
# date = message.CreationTime
# sender = message.SenderEmailAddress
# # S = message.SendUsingAccount
# SenderName = message.SenderName
# # print(S)
# print(SenderName)
# print(sender)
# print(date)
# print(subject)
# categories = message.Categories
# print(body_content)
# print(subject)
# print(categories)
# for e in email_items:
# print(email_items.subject)