zoukankan      html  css  js  c++  java
  • python学习之pypandoc

    对于程序员来说,文件格式之间的转换是一件非常费劲的事!比如md文件转化为html文件。

    于是乎,就有一群牛人搞出了个神器,他就是pandoc。

    而python中,对应的第三方模块就是pypandoc。

    下面就来看看,pandoc的安装以及使用:

    安装

    1.安装pandoc程序

    传送门:https://github.com/jgm/pandoc/releases/tag/1.19.2.1

    windows下直接下载.msi文件即可,安装就不说了。

    2.安装pypandoc

    pip install pypandoc

    使用

    下面的例子,将md文件转化为html文件:

    # -*- coding: utf-8 -*-
    #
    def readme():
        """
        转化文件的格式。
        convert(source, to, format=None, extra_args=(), encoding='utf-8', outputfile=None, filters=None)
        parameter-
            source:源文件
            to:目标文件的格式,比如html、rst、md等
            format:源文件的格式,比如html、rst、md等。默认为None,则会自动检测
            encoding:指定编码集
            outputfile:目标文件,比如test.html(注意outputfile的后缀要和to一致) 
        """
        try:
            import pypandoc
            return pypandoc.convert('README.md', 'html', format='md',outputfile='1.html')
        except (IOError, ImportError):
            with open('README.md') as f:
                return f.read()
    readme()
    

     在cmd中执行

    python test.py

    之后在md文件所在目录就会创建一个1.html文件。

    麻烦的文件转化工作,从此如此愉快!

  • 相关阅读:
    洛谷P2158 [SDOI2008]仪仗队 欧拉函数的应用
    leetcode 130. 被围绕的区域 DFS
    TediousLee CodeForces
    AccurateLee双指针+贪心+字符串
    leetcode80. 删除排序数组中的重复项 II
    CHFDORA:哆啦 A 梦
    cdq分治浅谈
    leetcode面试题64. 求1+2+…+n
    leetcode84. 柱状图中最大的矩形
    leetcode874. 模拟行走机器人
  • 原文地址:https://www.cnblogs.com/leomei91/p/7649547.html
Copyright © 2011-2022 走看看