Untitled
unknown
plain_text
a year ago
2.6 kB
5
Indexable
from email.mime.application import MIMEApplication import openpyxl from openpyxl.utils import get_column_letter from openpyxl.styles import Font from faker import Faker import random from datetime import datetime import os import smtplib from email.mime.multipart import MIMEMultipart def generate_random_names(num_names): fake = Faker() names = [fake.name() for _ in range(num_names)] return names def generate_excel_file(output_dir, num_rows): workbook = openpyxl.Workbook() sheet = workbook.create_sheet(title="TDSheet") headers = ["Имя", "Текущая дата", "Текущее время"] for col_num, header in enumerate(headers, 1): col_letter = get_column_letter(col_num) sheet[f"{col_letter}1"] = header sheet[f"{col_letter}1"].font = Font(bold=True) names = generate_random_names(num_rows) for row_num in range(2, num_rows + 2): name = names[row_num - 2] current_date = datetime.now().strftime("%Y-%m-%d") current_time = datetime.now().strftime("%H:%M:%S") sheet[f"A{row_num}"] = name sheet[f"B{row_num}"] = current_date sheet[f"C{row_num}"] = current_time file_name = f"{name}_{current_date}_{random.randint(100, 999)}.xlsx" file_path = os.path.join(output_dir, file_name) workbook.save(file_path) return file_path def send_email(file_path, to_email, your_email, password): print(file_path) msg = MIMEMultipart() msg["From"] = your_email msg["To"] = to_email msg["Subject"] = f"Message with file path: {file_path}" with open(file_path, "rb") as attachment: part = MIMEApplication(attachment.read(), Name=os.path.basename(file_path)) part['Content-Disposition'] = f'attachment; filename="{os.path.basename(file_path)}"' msg.attach(part) with smtplib.SMTP("smtp.gmail.com", 587) as connection: connection.starttls() connection.login(user=your_email, password=password) connection.sendmail(your_email, to_email, msg.as_string()) print(msg.as_string()) if __name__ == "__main__": to_email = "batyrhandastan79@gmail.com" your_email = "dastanbatyrkhan75@gmail.com" password = "jqud pyyu utpc dqqf" output_directory = r'C:\Users\Admin\PycharmProjects\pythonProject41' num_rows = 10 generated_file_path = generate_excel_file(output_directory, num_rows) send_email(generated_file_path, to_email, your_email, password) print(f" {generated_file_path}")
Editor is loading...
Leave a Comment