本文目录导读:

我来介绍几种将文本转换为脑图(思维导图)的脚本方法:
Markdown + Markmap(推荐)
使用Python脚本生成HTML
import json
def text_to_markmap(text, output_file="mindmap.html"):
"""将文本转换为markmap格式的脑图"""
# 解析文本为markdown格式
lines = text.strip().split('\n')
md_content = "# 思维导图\n"
for line in lines:
if ':' in line:
# 识别层级关系
parts = line.split(':')
depth = line.count(' ') # 根据缩进判断层级
prefix = ' ' * depth + '- '
md_content += f"{prefix}{parts[0]}\n"
for item in parts[1].split(','):
md_content += f"{' ' * (depth+1)}- {item.strip()}\n"
else:
md_content += f"- {line}\n"
# 生成HTML
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://cdn.jsdelivr.net/npm/markmap-autoloader"></script>
</head>
<body>
<div class="markmap">
{md_content}
</div>
</body>
</html>
"""
with open(output_file, 'w', encoding='utf-8') as f:
f.write(html)
print(f"脑图已生成: {output_file}")
# 使用示例
text = """
项目规划: 需求分析, 设计, 开发, 测试
需求分析: 用户访谈, 竞品分析, 功能列表
设计: UI设计, 架构设计, 数据库设计
开发: 前端开发, 后端开发, API开发
测试: 单元测试, 集成测试, 验收测试
"""
text_to_markmap(text)
使用 Node.js + mindmap 库
JavaScript脚本
const fs = require('fs');
const mindmap = require('mindmap');
function convertToMindmap(inputFile, outputFile) {
// 读取文本文件
const text = fs.readFileSync(inputFile, 'utf-8');
// 解析文本格式
const topics = parseText(text);
// 生成mindmap数据
const mindmapData = {
format: "node_map",
data: topics
};
// 保存为JSON
fs.writeFileSync(outputFile, JSON.stringify(mindmapData, null, 2));
console.log(`脑图数据已保存到: ${outputFile}`);
}
function parseText(text) {
const lines = text.split('\n');
const root = { id: 'root', text: '中心主题', children: [] };
const stack = [{ node: root, level: -1 }];
lines.forEach(line => {
if (!line.trim()) return;
const level = line.search(/\S/);
const text = line.trim();
const newNode = { id: generateId(), text, children: [] };
while (stack.length > 1 && stack[stack.length-1].level >= level) {
stack.pop();
}
stack[stack.length-1].node.children.push(newNode);
stack.push({ node: newNode, level });
});
return root;
}
let idCounter = 0;
function generateId() {
return `node_${idCounter++}`;
}
Python + XMind 格式
import xml.etree.ElementTree as ET
from xml.dom import minidom
def create_xmind(text, output_file="mindmap.xmind"):
"""生成XMind格式的脑图"""
# 创建XML结构
xmind = ET.Element("xmap-content", {"xmlns": "urn:xmind:xmap:xmlns:content:2.0"})
sheet = ET.SubElement(xmind, "sheet")
topic = ET.SubElement(sheet, "topic")= ET.SubElement(topic, "title")text = "思维导图"
# 解析文本并添加子节点
lines = text.strip().split('\n')
for line in lines:
if '-' in line:
parts = line.split('-')
depth = line.count(' ')
text = parts[-1].strip()
# 添加子主题
add_topic(topic, text, depth)
# 格式化XML
xml_str = minidom.parseString(ET.tostring(xmind)).toprettyxml()
with open(output_file, 'w', encoding='utf-8') as f:
f.write(xml_str)
print(f"XMind文件已生成: {output_file}")
def add_topic(parent, text, depth):
"""递归添加主题"""= ET.SubElement(parent, "title")text = text
简单文本格式示例
def simple_text_mindmap(input_file="input.txt"):
"""从简单文本生成脑图"""
with open(input_file, 'r', encoding='utf-8') as f:
content = f.read()
# 支持多种分隔符:|, TAB, 逗号
lines = content.split('\n')
structure = []
for line in lines:
if not line.strip():
continue
# 检测层级
depth = 0
for char in line:
if char in [' ', '\t', '|']:
depth += 1
else:
break
text = line.strip().lstrip('|-')
if ':' in text:
# 带子节点的项
main, sub = text.split(':', 1)
structure.append({
'depth': depth,
'text': main.strip(),
'children': [s.strip() for s in sub.split(',')]
})
else:
structure.append({
'depth': depth,
'text': text.strip(),
'children': []
})
return structure
# 使用示例
input_text = """
主题: 计算机科学
编程语言: Python, Java, JavaScript
数据结构: 数组, 链表, 树
树: 二叉树, 平衡树, B树
算法: 排序, 搜索, 动态规划
"""
with open('input.txt', 'w', encoding='utf-8') as f:
f.write(input_text)
result = simple_text_mindmap('input.txt')
print(json.dumps(result, ensure_ascii=False, indent=2))
在线工具结合脚本
import requests
def text_to_mindmap_api(text, api_url="https://api.mindmap.com/convert"):
"""使用在线API转换(示意代码)"""
data = {
"text": text,
"format": "markdown",
"output": "svg" # 或 "png", "pdf"
}
response = requests.post(api_url, json=data)
if response.status_code == 200:
with open("mindmap.svg", "wb") as f:
f.write(response.content)
print("脑图图片已保存")
else:
print(f"转换失败: {response.status_code}")
# 使用示例
text = """
# 读书计划
- 技术书籍
- Python基础
- 算法导论
- 文学类
- 百年孤独
- 活着
"""
text_to_mindmap_api(text)
使用建议
- 简单需求:使用Markmap方式,只需安装Python环境
- 专业需求:使用XMind格式,可以导入专业工具编辑
- 快速测试:用JS脚本在浏览器中运行
- 批量处理:用Python脚本处理大量文件
选择哪种方式取决于你的具体需求和技术栈。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播