当前位置:   article > 正文

文字内容自动生成PPT_生成ppt api

生成ppt api

第一次需求:

  1. 输入一段文字;
  2. 识别文字标题;
  3. 根据文字内容自动将文字分成段落;
  4. 生成PPT文件;
  5. 将文字标题放入PPT文件的第一页;
  6. 将文字内容按段落内容放置在PPT 文件的不同页面上 ;
  7. 将文字标题作为PPT文件名;
  8. 根据每页PPT内容 进行配图;
  9. 将PPT文件保存下来;
  1. import nltk
  2. from pptx import Presentation
  3. # 获取用户输入的文本内容
  4. text = input("请输入文本内容:")
  5. # 使用NLTK库将文章内容分成段落。
  6. import nltk
  7. nltk.download('punkt')
  8. def segment_paragraphs(text):
  9. tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
  10. paragraphs = tokenizer.tokenize(text)
  11. return paragraphs
  12. # 使用PyPPTX库创建一个新的PPT文件,并添加页面和文本框。
  13. from pptx import Presentation
  14. from pptx.util import Inches
  15. def create_ppt_file(title, paragraphs):
  16. prs = Presentation()
  17. # 添加标题页
  18. title_slide_layout = prs.slide_layouts[0]
  19. slide = prs.slides.add_slide(title_slide_layout)
  20. title_textbox = slide.shapes.title
  21. title_textbox.text = title
  22. # 添加段落页
  23. bullet_slide_layout = prs.slide_layouts[1]
  24. for paragraph in paragraphs:
  25. slide = prs.slides.add_slide(bullet_slide_layout)
  26. textbox = slide.shapes.placeholders[1].text_frame
  27. textbox.text = paragraph
  28. return prs
  29. # 使用Google Cloud Natural Language API识别文章标题。
  30. import os
  31. from google.cloud import language_v1
  32. os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/credentials.json'
  33. def get_document_title(text):
  34. client = language_v1.LanguageServiceClient()
  35. document = language_v1.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
  36. response = client.classify_text(request={'document': document})
  37. categories = response.categories
  38. if len(categories) > 0:
  39. return categories[0].name
  40. else:
  41. return 'Untitled'
  42. # 使用Unsplash API搜索关键词并下载图像。
  43. import requests
  44. import json
  45. import os
  46. UNSPLASH_ACCESS_KEY = 'your_access_key'
  47. def download_image(query, filename):
  48. url = 'https://api.unsplash.com/photos/random?query={}&client_id={}'.format(query, UNSPLASH_ACCESS_KEY)
  49. response = requests.get(url)
  50. data = json.loads(response.content.decode('utf-8'))
  51. image_url = data['urls']['regular']
  52. response = requests.get(image_url)
  53. with open(filename, 'wb') as f:
  54. f.write(response.content)
  55. # 整合以上所有功能,编写主函数。
  56. def auto_generate_ppt(text):
  57. # 分段落
  58. paragraphs = segment_paragraphs(text)
  59. # 识别标题
  60. title = get_document_title(text)
  61. # 创建PPT文件并添加页面和文本框
  62. prs = create_ppt_file(title, paragraphs)
  63. # 下载配图
  64. for i, paragraph in enumerate(paragraphs):
  65. filename = '{}_{}.jpg'.format(title, i+1)
  66. download_image(paragraph, filename)
  67. slide = prs.slides[i+1]
  68. slide.shapes.add_picture(filename, Inches(0.5), Inches(1), width=Inches(9), height=Inches(5))
  69. # 保存PPT文件
  70. prs.save('{}.pptx'.format(title))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/830026
推荐阅读
相关标签
  

闽ICP备14008679号