Skip to main content

可以将Word文档(仅.docx)、Excel表格、Html网页、PPt文件 转化为Markdown文件。

Project description

简体中文 | English

indexdoc-converter 文档转换工具库

indexdoc-converter 是一款基于 Python 开发的文档转换工具库,核心功能为将主流办公文档、网页文件高效转换为 Markdown 格式。各类型文件支持格式如下:

  • Word 文档支持 .docx
  • Excel 类表格文档支持 .xlsx、.xls、.ods、.csv、.tsv
  • 网页文件支持 .html、.mhtml、.htm 及网页url
  • PPT 演示文档支持 .pptx 。 该工具库现已发布至 PyPI(Python Package Index),可通过 pip 包管理工具快速安装并投入使用。

Python Version GitHub Stars

库的使用

#库安装
pip install -U indexdoc-converter #下载最新版本库
  • 若使用该库 python版本最小应为 Python3.10
  • 包目录结构
indexdoc-converter/          # 项目根目录
├── indexdoc_converter/      # 核心包目录   ├── __init__.py          # 核心代码   ├── docx_to_md.py        # Word转Markdown工具类   ├── excel_to_md.py       # Excel转Markdown工具类   ├── html_to_md.py        # Html转Markdown工具类   ├── pptx_to_md.py        # ppt转Markdown工具类   └── utils/
│       ├── __init__.py
│       ├── FileUtil.py
│       ├── IDUtil.py
│       └── img_to_base64.py

使用示例

#引用 注意引用为 indexdoc_converter 而不是 indexdoc-converter
from indexdoc_converter.docx_to_md import convert_docx_to_md
from indexdoc_converter.excel_to_md import TableToMarkdown
from indexdoc_converter.html_to_md import convert_to_md
from indexdoc_converter.pptx_to_md import pptx_to_md

 # -------------------------------------------Word转Markdown---------------------------------------------------
    md_text = convert_docx_to_md(r"C:\Users\xxx\测试文档.docx", False)
    with open('./test.md', 'w', encoding='utf-8') as f:
        f.write(md_text)

# -------------------------------------------Excel转Markdown-------------------------------------------------
    # 自定义参数示例
    converter = TableToMarkdown(
        file_title_level=2,  # 文件标题的Markdown层级,默认1(#),这里设为2(##)
        single_row_value_as_title=True,  # 是否将单行唯一值识别为标题,默认True
        max_rows=8000,  # 最大处理行数,默认6000(实际处理行数是max_rows+1)
        max_cols=200  # 最大处理列数,默认128(实际处理列数是max_cols+1)
    )

    # 转换单个文件
    file_path = r"C:\Users\xxx\测试文件.xlsx"
    result = converter.convert(file_path)

    # blank 模式:保留合并单元格的原始样式(只在合并单元格左上角显示内容,其余位置为空)
    with open("../tmp/测试_blank.md", "w", encoding="utf-8") as f:
        f.write(result['blank'])

   # fill 模式:将合并单元格的内容填充到所有合并的单元格中同时还能自动识别表格中的标题行、分割多个表格块,处理空行 / 空列,兼容各种表格格式的合并单元格解析。
    with open("../tmp/测试_fill.md", "w", encoding="utf-8") as f:
        f.write(result['fill'])

# -------------------------------------------ppt转Markdown---------------------------------------------------
    ppt_file = r"C:\Users\xxx\测试文件.pptx"
    md_path = pptx_to_md(ppt_file)
    print(f"单文件转换完成,MD文件路径:{md_path}")

# -------------------------------------------网页文件转Markdown-----------------------------------------------
    # html = "https://news.qq.com/rain/a/20260114A01NI000"
    html = "https://www.aituple.com"
    # html = "https://www.indexdoc.com"
    # html = r"C:\Users\xxx\测试文件.html"
    # html = "https://www.indexdoc.com/contact.html"
    md = convert_to_md(html, '../tmp/测试html.md')
    # md = mhtml_to_markdown(mhtml)

Word文档

原文档

主页1

转换后文档

主页1

Excel文档

原文档

主页1

转换后文档

主页1

ppt文档

原文档

主页1

转换后文档

主页1

网页文件

原文档

主页1

转换后文档

主页1

二次开发

  • Python 3.10+
#源码地址
https://github.com/indexdoc/indexdoc-converter.git
#快速安装依赖库
pip install -r requirements.txt

# 阿里镜像源
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

📞 作者

  • 作者:杭州智予数信息技术有限公司

  • 邮箱:indexdoc@qq.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

indexdoc_converter-0.2.4.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

indexdoc_converter-0.2.4-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

File details

Details for the file indexdoc_converter-0.2.4.tar.gz.

File metadata

  • Download URL: indexdoc_converter-0.2.4.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for indexdoc_converter-0.2.4.tar.gz
Algorithm Hash digest
SHA256 1a437dfa24deba3455bacc6458bf8e306b3e7dc9e95253492921a909f587a70e
MD5 00f197d91e163cb48c675946e3f90210
BLAKE2b-256 27258af6b67888906596d70f9a69c850097a4562d5a6882a18571be050db57a7

See more details on using hashes here.

File details

Details for the file indexdoc_converter-0.2.4-py3-none-any.whl.

File metadata

File hashes

Hashes for indexdoc_converter-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 93893cabfdf437c19f929dcd3db72b27db1fc7fa7901c72e1de953ffdc2688f2
MD5 c6219e34eee65e8a281c736e8f331a69
BLAKE2b-256 4bb2a7578fb6a8850b53ca62476c9d86e76f30bedb9d426bceac6f076db0ea42

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page