美文网首页
Pandoc docx to markdown

Pandoc docx to markdown

作者: DjangoUnchained | 来源:发表于2020-06-17 17:01 被阅读0次

介绍

本篇文档介绍如何将docx文档转换成markdown,并会介绍与python如何结合使用Pandoc的过滤

环境安装

Word文档准备

  • Word文档里的标题使用统一的标准
  • 文档里的图片转换成png/jpg等Markdown支持的格式

基本使用

pandoc -s 02_.docx -f docx -t markdown_phpextra -o markdown_php.md --extract-media=./ --wrap=none
详细参数释义请参阅官方文档https://pandoc.org/getting-started.html

高级使用

普通的转换可能无法满足我们的特殊需求或者对于一些文本的默认处理会有错误,因此需要结合自己写一些脚本来处理。
这里是以python为过滤器书写脚本,wordtomd_filter.py

# 这段代码处理转换后的图片无法正常显示的问题
from pandocfilters import toJSONFilters, Image

incomment = True


def comment(key, value, fmt, meta):
    """
    删除 【文档说明之前的行】
    """
    global incomment
    if key == 'Header':
        if "文档说明" in value[1]:
            incomment = False
            return None

    if incomment:
        return []

def due_images(key, value, fmt, meta):
    """
    """
    if key == "Image":
        value[0][2] = []

        for _ in value[1]:
            if _["t"] == 'Space':
                del _
            else:
                _["c"] = ''

        return Image(value[0], value[1], value[2])

if __name__ == "__main__":
    toJSONFilters([comment, due_images])

执行命令行: pandoc -s 02_.docx -f docx -t markdown_phpextra -o markdown_php.md --extract-media=./ --wrap=none -F wordtomd_filter.py

相关文章

网友评论

      本文标题:Pandoc docx to markdown

      本文链接:https://www.haomeiwen.com/subject/ijvsxktx.html