Yes, I can build a Python system that can auto unzip all the .zip files in the specified directory and perform the necessary operations as per the requirements mentioned in the statement. Here's the solution:
import os
import zipfile
import smtplib
from email.message import EmailMessage
import xml.etree.ElementTree as ET
def unzip_files(dir_path):
zip_files = [f for f in os.listdir(dir_path) if f.endswith('.zip')] # get all .zip files in directory
for zip_file in zip_files:
zip_file_path = os.path.join(dir_path, zip_file)
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(os.path.join(dir_path, 'unzipped')) # extract the zip file to './unzipped' directory
def send_email(to_email, subject, body, attachment_file):
msg = EmailMessage()
msg.set_content(body)
msg['Subject'] = subject
msg['To'] = to_email
with open(attachment_file, 'rb') as f:
file_data = f.read()
file_name = os.path.basename(attachment_file)
msg.add_attachment(file_data, maintype='application', subtype='pdf', filename=file_name)
with smtplib.SMTP('localhost', 1025) as smtp:
smtp.send_message(msg)
def check_fsd(xml_file_path, library):
tree = ET.parse(xml_file_path)
root = tree.getroot()
fsd = root.find('./data/applicant/fsd').text # find the fsd value in xml file
for data in library:
if fsd == data[0]: # compare fsd with the library data
to_email = data[1]
subject = "PDF Attachment"
body = "Please find attached pdf file."
attachment_file = os.path.join(os.path.dirname(xml_file_path), 'document.pdf')
send_email(to_email, subject, body, attachment_file) # send email with pdf attachment
def process_unzipped_files(dir_path, library):
unzipped_files = [f for f in os.listdir(os.path.join(dir_path, 'unzipped')) if os.path.isdir(os.path.join(dir_path, 'unzipped', f))]
for unzipped_file in unzipped_files:
xml_file_path = os.path.join(dir_path, 'unzipped', unzipped_file, 'document.xml')
pdf_file_path = os.path.join(dir_path, 'unzipped', unzipped_file, 'document.pdf')
if os.path.isfile(xml_file_path) and os.path.isfile(pdf_file_path):
check_fsd(xml_file_path, library) # check for fsd and send email with pdf attachment
if __name__ == '__main__':
dir_path = './unzip'
library = [["aaa","aaa@email.com"], ["bbb","bbb@email.com"], ["ccc","ccc@email.com"]]
unzip_files(dir_path)
process_unzipped_files(dir_path, library)
Here's how the code works:
-
The unzip_files function takes the directory path as an input, gets all the .zip files from the directory, and extracts each file to the "./unzipped" directory.
-
The send_email function takes the recipient email, subject line, body message, and attachment file path as inputs. It creates an EmailMessage object and sets the content, subject, and recipient fields. It reads the attachment file as binary data and adds it to the message as an attachment. Finally, it sends the email through the SMTP server running on port 1025 (a local SMTP debugging server).
-
The check_fsd function takes the XML file path and library as inputs. It uses the ElementTree module to parse the XML file, finds the "fsd" value in the XML file, and checks whether it matches with the "fsd" value in the library. If it matches, it gets the corresponding email address from the library and sends an email with the PDF attachment using the send_email function.
-
The process_unzipped_files function takes the directory path and library as inputs. It gets all the unzipped folders from the "./unzipped" directory, checks whether they contain both the "document.xml" and "document.pdf" files, and processes each folder using the check_fsd function.
-
Finally, in the main function, we initialize the directory path and library, call the unzip_files function to extract the zip files, and call the process_unzipped_files function to process each unzipped folder.
Note: In the send_email function, you need to update the SMTP server settings according to your email provider's settings. Also, you may need to install the elementtree module and set up a local SMTP debugging server to test the email functionality.