def outlookItem(fname): cv_SPOT_No_Processed = [] newCont = '' line = '' flag = 0 with open(fname) as msg_file: msg = Message(msg_file) content = msg.body for c in content: if c != '\n' and c != '\r': line += c else: flag = 1 pass if flag == 1: if c != '\n' and c != '\r': lastChar = line[-1] line = line[:-1] line = line + '\n' line = line + lastChar flag = 0 else: pass line = line.split('\n') line = list(map(lambda x: x.upper(), line)) for ele in range(len(line)): if '\t' in line[ele]: line.insert(ele, ' ') line[ele + 1] = line[ele + 1].replace('\t', '') data = [] fare = [] comData = [] fullData = [] for i in range(len(line)): if '$' in line[i]: fare.append(i) lastFareIndex = fare[-1] if 'NÚM SP ID OTM' in line: start = line.index('NÚM SP ID OTM') + 10 for i in range(start, lastFareIndex + 1, 10): singleData = [ line[i], line[i + 1], line[i + 2], line[i + 3], line[i + 4], line[i + 5], line[i + 6], line[i + 7], line[i + 8], line[i + 9] ] if ' ' in singleData: cv_SPOT_No_Processed.append(line[i + 6]) else: comData.append(singleData) for i in comData: fareInd = i[-1].replace(' ', '') fareInd = fareInd.replace('$', '') fareInd = fareInd.replace(',', '') fareInd = fareInd[0:-3] i.pop(-1) i.insert(len(i), fareInd) fullData.append(comData) comData.append(cv_SPOT_No_Processed) return comData
def pick_message(): try: root = Tk() root.withdraw() root.call('wm', 'attributes', '.', '-topmost', True) filename = filedialog.askopenfilename(filetypes = [('Outlook Messages', '*.msg')]) print(filename) with open(filename) as msg_file: msg = Message(msg_file) return msg except: pass
## OPEN TEXT FILE AND EXTRACT DATA files = [ f for f in listdir( "G:\\Customer Reporting\\438-DVS\\Sales Leads Automation\\") if isfile("G:\\Customer Reporting\\438-DVS\\Sales Leads Automation\\%s" % f) ] if len(files) == 0: sys.exit(0) with open("G:\\Customer Reporting\\438-DVS\\Sales Leads Automation\\%s" % files[0]) as msg_file: msg = Message(msg_file) msg_file.close() contents = msg.body lines = contents.split('\n') headings = [] for i in range(len(lines)): line = lines[i] if "Name" == line[:4] or "Name:" == line[:5]: contact = lines[i + 1].strip() elif "Email" == line[:5] or "Email:" == line[:6]: email = lines[i + 1].strip() try:
def message() -> Message: with open('test_data/Test.msg') as f: return Message(f)
def message_no_attachment() -> Message: with open('test_data/No attachment.msg') as f: return Message(f)