第一次需求:
- 输入一段文字;
- 识别文字标题;
- 根据文字内容自动将文字分成段落;
- 生成PPT文件;
- 将文字标题放入PPT文件的第一页;
- 将文字内容按段落内容放置在PPT 文件的不同页面上 ;
- 将文字标题作为PPT文件名;
- 根据每页PPT内容 进行配图;
- 将PPT文件保存下来;
import nltk
from pptx import Presentation# 获取用户输入的文本内容
text = input("请输入文本内容:")# 使用NLTK库将文章内容分成段落。import nltknltk.download('punkt')def segment_paragraphs(text):tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')paragraphs = tokenizer.tokenize(text)return paragraphs# 使用PyPPTX库创建一个新的PPT文件,并添加页面和文本框。
from pptx import Presentation
from pptx.util import Inchesdef create_ppt_file(title, paragraphs):prs = Presentation()# 添加标题页title_slide_layout = prs.slide_layouts[0]slide = prs.slides.add_slide(title_slide_layout)title_textbox = slide.shapes.titletitle_textbox.text = title# 添加段落页bullet_slide_layout = prs.slide_layouts[1]for paragraph in paragraphs:slide = prs.slides.add_slide(bullet_slide_layout)textbox = slide.shapes.placeholders[1].text_frametextbox.text = paragraphreturn prs# 使用Google Cloud Natural Language API识别文章标题。
import os
from google.cloud import language_v1os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/credentials.json'def get_document_title(text):client = language_v1.LanguageServiceClient()document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)response = client.classify_text(request={'document': document})categories = response.categoriesif len(categories) > 0:return categories[0].nameelse:return 'Untitled'# 使用Unsplash API搜索关键词并下载图像。
import requests
import json
import osUNSPLASH_ACCESS_KEY = 'your_access_key'def download_image(query, filename):url = 'https://api.unsplash.com/photos/random?query={}&client_id={}'.format(query, UNSPLASH_ACCESS_KEY)response = requests.get(url)data = json.loads(response.content.decode('utf-8'))image_url = data['urls']['regular']response = requests.get(image_url)with open(filename, 'wb') as f:f.write(response.content)# 整合以上所有功能,编写主函数。
def auto_generate_ppt(text):# 分段落paragraphs = segment_paragraphs(text)# 识别标题title = get_document_title(text)# 创建PPT文件并添加页面和文本框prs = create_ppt_file(title, paragraphs)# 下载配图for i, paragraph in enumerate(paragraphs):filename = '{}_{}.jpg'.format(title, i+1)download_image(paragraph, filename)slide = prs.slides[i+1]slide.shapes.add_picture(filename, Inches(0.5), Inches(1), width=Inches(9), height=Inches(5))# 保存PPT文件prs.save('{}.pptx'.format(title))