Traceback (most recent call last): File "D:\xmind_1\excel.py", line 6, in <module> xmind_content = xmind.load(f) File "D:\python\lib\site-packages\xmind\__init__.py", line 14, in load loader = WorkbookLoader(path) File "D:\python\lib\site-packages\xmind\core\loader.py", line 22, in __init__ self._input_source = utils.get_abs_path(path) File "D:\python\lib\site-packages\xmind\utils.py", line 58, in get_abs_path fp, fn = os.path.split(path) File "D:\python\lib\ntpath.py", line 185, in split p = os.fspath(p) TypeError: expected str, bytes or os.PathLike object, not BufferedReader
时间: 2023-08-02 13:06:26 浏览: 292
这个错误提示说明在使用xmind库时,读取XMind文件的方式有问题,原因是`get_abs_path()`函数期望得到一个字符串、字节或`os.PathLike`对象,但是您提供的是一个`BufferedReader`对象。
您可以尝试使用以下代码,将`f`对象转换为字符串或字节对象:
```
import xmind
import io
# 打开XMind文件
xmind_file = 'your_xmind_file.xmind'
with open(xmind_file, 'rb') as f:
xmind_content = xmind.load(io.BytesIO(f.read()))
# 创建Excel工作簿和工作表
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('Sheet1')
# 写入Excel表头
worksheet.write(0, 0, '主题')
worksheet.write(0, 1, '子主题')
# 遍历XMind内容,提取主题和子主题,并写入Excel文件
row = 1
for sheet in xmind_content[0]['topic']['topics']:
main_topic = sheet['title']
for sub_topic in sheet['topics']:
sub_topic_title = sub_topic['title']
worksheet.write(row, 0, main_topic)
worksheet.write(row, 1, sub_topic_title)
row += 1
# 保存Excel文件
workbook.save('your_excel_file.xls')
```
这段代码使用了Python标准库中的`io`模块,将`f`对象转换为`io.BytesIO`对象,这个对象可以被xmind库识别。请注意,这段代码仅适用于Python 3.x版本。
阅读全文