赞
踩
第一次需求:
- import nltk
- from pptx import Presentation
-
- # 获取用户输入的文本内容
- text = input("请输入文本内容:")
-
- # 使用NLTK库将文章内容分成段落。
-
- import nltk
-
- nltk.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 Inches
-
- def 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.title
- title_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_frame
- textbox.text = paragraph
-
- return prs
-
- # 使用Google Cloud Natural Language API识别文章标题。
- import os
- from google.cloud import language_v1
-
- os.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.categories
- if len(categories) > 0:
- return categories[0].name
- else:
- return 'Untitled'
-
- # 使用Unsplash API搜索关键词并下载图像。
- import requests
- import json
- import os
-
- UNSPLASH_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))
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。