本文目录导读:

- Python + python-pptx(最常用)
- Markdown 到幻灯片(更结构化)
- 使用 Google Apps Script(在线方案)
- Bash + Pandoc(命令行快速方案)
- 文本格式建议
- 安装依赖
我来介绍几种将文本转换为幻灯片的方法,使用不同脚本语言:
Python + python-pptx(最常用)
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.text import PP_ALIGN
def text_to_slides(text_file, output_file="slides.pptx"):
# 创建演示文稿
prs = Presentation()
# 读取文本文件
with open(text_file, 'r', encoding='utf-8') as f:
content = f.read()
# 按行拆分,以空行作为幻灯片分隔
slides_content = content.split('\n\n')
for slide_text in slides_content:
lines = slide_text.strip().split('\n')
if not lines:
continue
# 添加幻灯片
slide_layout = prs.slide_layouts[1] # 标题和内容布局
slide = prs.slides.add_slide(slide_layout)
# 设置标题(第一行)
title = slide.shapes.title
title.text = lines[0] if lines else ""
# 设置内容
content_shape = slide.placeholders[1]
tf = content_shape.text_frame
for i, line in enumerate(lines[1:], 1):
if line.strip():
p = tf.add_paragraph()
p.text = line.strip()
p.level = 0
prs.save(output_file)
print(f"幻灯片已保存到: {output_file}")
# 使用示例
text_to_slides("my_notes.txt")
Markdown 到幻灯片(更结构化)
import markdown
from pptx import Presentation
from pptx.util import Inches
def markdown_to_slides(md_file, output="slides.pptx"):
with open(md_file, 'r', encoding='utf-8') as f:
content = f.read()
# 按 ## 标题分割幻灯片
slides_text = content.split('\n## ')
prs = Presentation()
for slide_text in slides_text:
lines = slide_text.strip().split('\n')
slide_layout = prs.slide_layouts[6] # 空白布局
slide = prs.slides.add_slide(slide_layout)
# 添加文本框
left = top = Inches(1)
width = Inches(8)
height = Inches(5.5)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
for i, line in enumerate(lines):
if i == 0:
# 第一行作为标题
p = tf.paragraphs[0]
p.text = line.replace('# ', '')
else:
p = tf.add_paragraph()
if line.startswith('- '):
p.text = line[2:] # 去掉列表符号
p.level = 1
else:
p.text = line
prs.save(output)
使用 Google Apps Script(在线方案)
function textToSlides() {
// 获取文本(从Google Docs或其他来源)
var text = "幻灯片标题1\n内容1\n\n幻灯片标题2\n内容2";
// 创建新演示文稿
var presentation = SlidesApp.create('我的幻灯片');
// 按空行分割
var slides = text.split('\n\n');
slides.forEach(function(slideText) {
var lines = slideText.split('\n');
if (lines.length > 0) {
var slide = presentation.appendSlide(SlidesApp.PredefinedLayout.TITLE_AND_BODY);
// 设置标题
slide.getShapes()[0].getText().setText(lines[0]);
// 设置内容
var body = slide.getShapes()[1].getText();
for (var i = 1; i < lines.length; i++) {
if (lines[i].trim()) {
body.appendParagraph(lines[i]);
}
}
}
});
}
Bash + Pandoc(命令行快速方案)
#!/bin/bash
# 将文本文件转换为幻灯片
# 格式要求:每张幻灯片用 --- 分隔
input_file="$1"
output_file="${2:-slides.pptx}"
# 转换为Markdown格式再转为PPTX
pandoc "$input_file" -t pptx -o "$output_file"
echo "幻灯片已生成: $output_file"
文本格式建议
为了更好的转换效果,建议文本格式:
# 第1张幻灯片标题
- 要点1
- 要点2
- 要点3
# 第2张幻灯片标题支持多行文本
# 第3张幻灯片标题
1. 有序列表项
2. 有序列表项
3. 有序列表项
安装依赖
# Python方案 pip install python-pptx markdown # Bash方案 sudo apt-get install pandoc # Linux brew install pandoc # Mac
选择建议:
- 简单文本 → Python + python-pptx
- Markdown格式 → Pandoc
- 在线协作 → Google Apps Script
- 批量处理 → Bash脚本
需要我提供更具体的使用示例吗?