사이드 프로젝트 도전기/etc

Excel, PPT, Word, PDF 더미 파일 생성기

Developer D 2024. 11. 8. 15:57
반응형

테스트파일을 만들일이 필요해서 간단하게 제작한 MS Office 파일 생성기

 

조건

1.파일 이름 넘버링

2.확장자 뿐만 아니라 실제 각 오피스파일 형식에 맞아야함

3.크기 랜덤 (내용 랜덤)

 

결과

1.파일 이름 넘버링 

2.실행 시 현재 경로에서 실행

3.랜덤 파일 10,000개 생성 (Excel, PPT, Word, PDF)

 

create_MS_dummy_file.exe

create_MS_dummy_file.exe
20.00MB

 

create_MS_dummy_file.py

import os
import random
from pptx import Presentation
from docx import Document
from openpyxl import Workbook
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

# Number of files and folder settings
num_files = 10000
file_size_min = 1 * 1024 * 1024 
file_size_max = 100 * 1024 * 10240  
current_folder = os.getcwd()

# Generate random text with given size in MB
def generate_random_text(size_in_mb):
    text = ''.join(random.choices("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ", k=1024))
    return text * (size_in_mb * 1024 // len(text))  # Repeat text to reach target size

# Create PPTX file
def create_pptx(filename, target_size):
    prs = Presentation()
    slide_layout = prs.slide_layouts[1]
    slide = prs.slides.add_slide(slide_layout)
    title = slide.shapes.title
    content = slide.placeholders[1]
    
    title.text = "Generated PPTX File"
    content.text = generate_random_text(target_size // 1048576)  # Generate text 

    prs.save(filename)

# Create DOCX file
def create_docx(filename, target_size):
    doc = Document()
    doc.add_heading("Generated DOCX File", 0)
    doc.add_paragraph(generate_random_text(target_size // 1048576))
    
    doc.save(filename)

# Create XLSX file
def create_xlsx(filename, target_size):
    wb = Workbook()
    ws = wb.active
    ws.title = "Generated XLSX File"

    text = generate_random_text(target_size // 1048576)
    for i in range(1, 100):  # Row loop
        ws.append([text[:50] for _ in range(10)])  
    wb.save(filename)

# Create PDF file
def create_pdf(filename, target_size):
    c = canvas.Canvas(filename, pagesize=letter)
    width, height = letter
    text = generate_random_text(target_size // 1048576)
    
    y = height - 40  # Start from top
    for i in range(0, len(text), 1000):
        c.drawString(100, y, text[i:i+1000])  # Add text to page
        y -= 15
        if y < 40:
            c.showPage()
            y = height - 40  # Reset for new page
    
    c.save()

# Generate random files
for i in range(num_files):
    file_type = random.choice(["pptx", "docx", "xlsx", "pdf"])
    target_size = random.randint(file_size_min, file_size_max)

    if file_type == "pptx":
        filename = os.path.join(current_folder, f"file_{i+1}.pptx")
        create_pptx(filename, target_size)
    elif file_type == "docx":
        filename = os.path.join(current_folder, f"file_{i+1}.docx")
        create_docx(filename, target_size)
    elif file_type == "xlsx":
        filename = os.path.join(current_folder, f"file_{i+1}.xlsx")
        create_xlsx(filename, target_size)
    elif file_type == "pdf":
        filename = os.path.join(current_folder, f"file_{i+1}.pdf")
        create_pdf(filename, target_size)

    print(f"{filename} generated")

print(f"{num_files} files have been generated in the current folder.")

create_MS_dummy_file

반응형