створює pptx зі слайдами, що містять вміст кожного текстового файлу в папці, а заголовки слайдів будуть іменами цих файлів
import os
import glob
from pptx import Presentation
from pptx.util import Inches, Pt
def create_presentation_from_text_files(folder_path):
# Create a new presentation
prs = Presentation()
# Define slide layout
title_slide_layout = prs.slide_layouts[1] # Title and Content layout
# Get all text files in the folder
text_files = glob.glob(os.path.join(folder_path, "*.txt"))
if not text_files:
print(f"No text files found in {folder_path}")
return
# Process each text file
for file_path in text_files:
# Get filename without extension to use as slide title
filename = os.path.basename(file_path)
title = os.path.splitext(filename)[0]
# Read the content of the text file
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
except Exception as e:
print(f"Error reading {file_path}: {e}")
content = f"[Error reading file: {e}]"
# Create a new slide
slide = prs.slides.add_slide(title_slide_layout)
# Set the slide title
slide.shapes.title.text = title
# Add the content to the slide
content_shape = slide.placeholders[1]
text_frame = content_shape.text_frame
text_frame.text = content
# Format the text (optional)
for paragraph in text_frame.paragraphs:
paragraph.font.size = Pt(14)
# Save the presentation
output_path = os.path.join(folder_path, "text_files_presentation.pptx")
prs.save(output_path)
print(f"Presentation created successfully at: {output_path}")
# Get folder path from user
folder_path = input("Enter the folder path containing text files: ")
create_presentation_from_text_files(folder_path)