Python实现批量pandoc 将markdown md文件 转换为其他doc epub mobi html文件

import os

def get_file_name(file_dir):
    for root, dirs, files in os.walk(file_dir):
        # print(root)
        # print(dirs)
        # print(files)
        for file in files:
            if os.path.splitext(file)[1] == '.md':
                # print(root)
                os.chdir(root)
                print("pandoc " + file + ' -o ' + os.path.splitext(file)[0] + '.epub')
                os.system("pandoc " + file + ' -o ' + os.path.splitext(file)[0] + '.epub')


if __name__ == '__main__':
    get_file_name(r'C:\Users\17692\Desktop\ai-edu\6')

如需修改文件目录请修改第17行中的路径

代码如上,逻辑非常简单

获取当前文件夹的信息,扫描所有文件,提取出所有后缀名为md的文件,使用os.system调用pandoc进行格式转化。有关pandoc的下载及安装使用方法请自行查找。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python的第三方库`pandoc`和`python-pptx`来实现Markdown文件转换为PPT文件的功能。 首先需要安装这两个库,可以使用以下命令进行安装: ```bash pip install pandoc pip install python-pptx ``` 然后,可以使用以下代码将Markdown文件转换为PPT文件: ```python import subprocess import os from pptx import Presentation def convert_to_ppt(md_file, ppt_file): # Convert markdown to docx docx_file = md_file.split('.')[0] + '.docx' cmd = ['pandoc', md_file, '-o', docx_file] subprocess.call(cmd) # Convert docx to pptx prs = Presentation() slide_layout = prs.slide_layouts[1] cmd = ['libreoffice', '--headless', '--convert-to', 'pptx', docx_file, '--outdir', os.getcwd()] subprocess.call(cmd) pptx_file = docx_file.split('.')[0] + '.pptx' # Insert slides into pptx slides = Presentation(pptx_file).slides for slide in slides: prs_slide = prs.slides.add_slide(slide_layout) for shape in slide.shapes: el = shape.element newel = el.clone() prs_slide.shapes._spTree.insert_element_before(newel, 'p:extLst') # Save pptx file prs.save(ppt_file) # Remove temporary files os.remove(docx_file) os.remove(pptx_file) # Example usage convert_to_ppt('example.md', 'example.pptx') ``` 这段代码将Markdown文件转换docx文件,再将docx文件转换为pptx文件,最后将pptx文件中的所有幻灯片插入到新创建的PPT文件中,并保存为指定的文件名。注意,这里使用了LibreOffice来进行docx和pptx文件转换,因此需要在系统中安装LibreOffice并将其添加到系统路径中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值