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.3.tar.gz (27.1 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.3-py3-none-any.whl (27.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: indexdoc_converter-0.2.3.tar.gz
  • Upload date:
  • Size: 27.1 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.3.tar.gz
Algorithm Hash digest
SHA256 74bcc3a84fd88e09091f09ae41cb4edf6337beb4b0cfb447eac47a7f37e06f82
MD5 6c89ded28f8c475f453941f6a333907c
BLAKE2b-256 3364739a1e53f702963cf8138bf9122aa5295e0fb6d97fb8ec52308402e4a5f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for indexdoc_converter-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 081957a346158580b540c2657d460114ef05447a46d420e01f5964203952a831
MD5 79cfd0a039795a2e6677d03d0f1e05b7
BLAKE2b-256 d02a9aea6361c835f7e2169c269d6c55f0534a9ea9a854a369229048c82ff9b1

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