python遍历文件夹下所有内容,生成xmind脑图

一、安装依赖xmind

bash 复制代码
pip3 install xmind

二、生成xmind文件

python 复制代码
import xmind
import os

def scan_folder_to_xmind(folder_path, output_path):
    workbook = xmind.load(output_path)  # 创建或加载XMind文件
    sheet = workbook.getPrimarySheet()  # 获取主工作表
    root_topic = sheet.getRootTopic()
    root_topic.setTitle(os.path.basename(folder_path))
    
    def add_folder_contents(parent_topic, current_path):
        items = os.listdir(current_path)
        for item in items:
            item_path = os.path.join(current_path, item)
            if os.path.isdir(item_path):
                folder_topic = parent_topic.addSubTopic()
                folder_topic.setTitle(f"📁 {item}")
                add_folder_contents(folder_topic, item_path)
            else:
                file_topic = parent_topic.addSubTopic()
                file_topic.setTitle(f"📄 {item}")
    
    add_folder_contents(root_topic, folder_path)
    xmind.save(workbook, output_path)  # 保存文件
    print(f"✅ 思维导图已生成: {output_path}")

# 使用示例
scan_folder_to_xmind('/path/to/folder', 'output.xmind')

三、问题

上面的output.xmind通过Xmind应用程序,无法正常打开。

四、原因

  • 文件格式不兼容‌:生成的文件可能是 XMind 8 格式,而您使用的是 XMind 2020 或更高版本,导致无法打开。
  • 缺少关键文件‌:XMind 2020+ 需要 META-INF/manifest.xml 文件,而生成的文件可能缺失此文件。

五、修复文件结构

为了兼容 XMind 2020+,需要修改文件结构

python 复制代码
import zipfile
import os

def fix_xmind_format(file_path):
    # 修改后缀为zip
    zip_path = file_path + '.zip'
    os.rename(file_path, zip_path)
    
    # 解压文件
    with zipfile.ZipFile(zip_path, 'r') as zip_ref:
        zip_ref.extractall(os.path.splitext(zip_path)[0])
    
    # 添加META-INF/manifest.xml
    manifest_content = """<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns="http://www.xmind.net/manifest">
    <version>1.0</version>
</manifest>"""
    manifest_path = os.path.join(os.path.splitext(zip_path)[0], 'META-INF', 'manifest.xml')
    os.makedirs(os.path.dirname(manifest_path), exist_ok=True)
    with open(manifest_path, 'w', encoding='utf-8') as f:
        f.write(manifest_content)
    
    # 重新压缩为zip
    with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_ref:
        for root, dirs, files in os.walk(os.path.splitext(zip_path)[0]):
            for file in files:
                zip_ref.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), os.path.splitext(zip_path)[0]))
    
    # 恢复后缀为xmind
    os.remove(file_path)
    os.rename(zip_path, file_path)
    print(f"✅ 文件格式已修复: {file_path}")

# 使用示例
fix_xmind_format('output.xmind')

最后,通过xmind,正常打开目录结构

相关推荐
天下无贼9 小时前
【Python】2026版——FastAPI 框架快速搭建后端服务
后端·python·aigc
小蚂蚁i9 小时前
LangChain 完全学习手册:看完就能上手
后端·python·ai编程
Aawy12010 小时前
Python生成器(Generator)与Yield关键字:惰性求值之美
jvm·数据库·python
沐硕10 小时前
《基于改进协同过滤与多目标优化的健康饮食推荐系统设计与实现》
java·python·算法·fastapi·多目标优化·饮食推荐·改进协同过滤
乱世军军10 小时前
把 Python 3.13 降级到 3.11
开发语言·python
Y5neKO10 小时前
某国赛CTF逆向题目Writeup:re1
python·逆向·ctf
带娃的IT创业者11 小时前
WeClaw 架构演进史:从 0 到 1 构建跨平台 AI 助手的完整历程
人工智能·python·websocket·架构·fastapi·架构设计·实时通信
Storynone11 小时前
【Day28】LeetCode:509. 斐波那契数,70. 爬楼梯,746. 使用最小花费爬楼梯
python·算法·leetcode
guts35012 小时前
使用python里的OpenCV包做简单的车道线检测
人工智能·python·opencv
sz-lcw12 小时前
HOG特征向量计算方法
人工智能·python·算法