Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.5 kB
1
Indexable
Never
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

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="TDdSheet")


    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"
    print(output_dir)
    file_path = os.path.join(output_dir, file_name)
    workbook.save(file_path)
    return file_path
output_directory = r'C:\Users\Admin\PycharmProjects\pythonProject41'
num_rows = 10
generated_file_path = generate_excel_file(output_directory, num_rows)
print(f" {generated_file_path}")
Leave a Comment