Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting CSV file inside ZIP file from mails' attachments in one go using Python

I found and modified a code to read CSV attachments file from Outlook application using Python.

What happened in my case is: When I request data for a certain period, they will send me the monthly data of the requested period in separate emails (E.g. request: January 2018 - December 2018; Receive : 12 mails with a single CSV attachment in each one of them.) i save all of the emails coming from the data warehouse in 'DWH Mail'

All of the emails will come from the same subject. So my code will: Save all CSV attachments which are stored in 'DWH Mail' from the Subject specified.

import win32com.client as client
import datetime as date
import os.path

def attach(mail_subject):
    outlook = client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    folder = outlook.GetDefaultFolder("6").Folders["DWH Mail"]

    val_date = date.date.today() 
    sub_target = mail_subject

    for msg in folder.Items:
        if msg.ReceivedTime.date() == val_date and msg.Subject == sub_target:
            for att in msg.Attachments:
                att.SaveASFile(os.getcwd() + "\\" + att.FileName)
                print ("Mail Successfully Extracted")
                break

    print ("Done")

Now I could request for ZIP file, containing the CSV, so that I could receive the file faster. Where and what should I add in my code so that the loop will extract and save the CSV file from the ZIP file? Instead of save the ZIP file and I extract it manually later.

I am relatively new to Python, so any helps would be appreciated. Thank you.

like image 491
James Hadimaja Avatar asked Nov 21 '25 11:11

James Hadimaja


1 Answers

import os
import pandas as pd
import zipfile

curDir = os.getcwd()
zf = zipfile.ZipFile(curDir + '/targetfolder/' + yourFileName + '.zip')
text_files = zf.infolist()
# list_ = []

print ("Decompressing and loading data into multiple files... ")

for text_file in text_files:
    print(text_file.filename)
    df = pd.read_csv(zf.open(text_file.filename)
    # do df manipulations if required
    df.to_csv(curDir + '/targetfolder/' + text_file.filename + '.csv')

# df = pd.concat(list_)

This will iterate through all the files and load them with the respective names as present in the zip file.