进击的PythonPython6.环境

Python将md批量转为docx

2018-05-10  本文已影响2119人  zhaoolee

这两天写毕业论文, 发现了一个可以将markdown快速转为word格式的小工具pandoc, 非常好用, 比如我有一个名为毕业论文.md的文件, 我只需在命令行运行

pandoc 毕业论文.md -o 毕业论文.docx

即可根据md文件生成新的docx文件!

pandoc支持相互转换的格式, 多的惊人!
pandoc

Pandoc主站链接

安装包下载地址

https://github.com/jgm/pandoc/releases/tag/2.2

使用技巧:

import os  

def auto_md_to_docx(file_dir):
    # 获取当前目录下所有的md文件的路径信息
    all_whole_path_files = []
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            try:
                if file[-3:] == ".md":
                    file_info = [root+'/', file]
                    all_whole_path_files.append(file_info)
            except Exception as e:
                print(e)
    print("==>", all_whole_path_files)

    # 将md依次转换为pandoc
    for file_info in all_whole_path_files:
        md_file_path_file = file_info[0] + file_info[1]
        docx_file_name = file_info[1][:-3] + '.docx'
        docx_file_path_file = file_info[0] + docx_file_name
        new_command = 'pandoc ' + md_file_path_file + ' -o ' + docx_file_path_file

        try:
            result = os.popen(new_command).readlines()
            if len(result) == 0:
                print(md_file_path_file, "已经转换为", docx_file_path_file)
        except Exception as e:
            print(e)

def main():
    auto_md_to_docx('.')

if __name__ == '__main__':
    main()


运行效果
最终结果

windows用户安装pandoc

1.下载32位免安装版软件包

下载地址: https://github.com/jgm/pandoc/releases/tag/2.2.1

2.将软件包放入c盘, 并解压

3.获取pandoc.exe的绝对路径

4. 将pandoc.exe的绝对路径放入环境变量

另外, 小白福利, 我把这篇博客做成了视频 https://www.bilibili.com/video/av24136955/
欢迎观看, 欢迎投币, 欢迎弹幕, 欢迎转发,,,

上一篇 下一篇

猜你喜欢

热点阅读