python代码实现md转html

今天git上下载了一个电子书,格式是MD格式的,想弄成HTML格式后看,就自动动手写了一个转成脚本。


md是markdown格式的,用markdown模块来转

command = 'C:\\Python27\\python.exe c:\\Python27\\Scripts\\markdown_py -o html5 -f '+htmlfile+" "+fullfilename


因为有中文,所以转换后的HTML文件开头加进去了

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
不加的话,打开中文是乱码。



#encoding=utf-8

from time import sleep
import os
import os.path

__author__ = {'author':'pygocc','qq':'644234366','createdate':'2015-09-10'}

rootdir = raw_input("请输入要转换的目录:")

def mdtohtml(fullfilename):
    try:
        htmlfile = fullfilename+".html"
        command = 'C:\\Python27\\python.exe c:\\Python27\\Scripts\\markdown_py -o html5 -f '+htmlfile+" "+fullfilename
        os.system(command)

        existhtml = False
        i = 0
        while(True):
            if os.path.isfile(htmlfile)==True:
                existhtml = True
                break

            if i>=5:
                print "tohtmlfile failed:",htmlfile
                break

            i = i+1
            sleep(0.1)

        if existhtml == True:
            fp = open(htmlfile,"r+")
            pos = fp.tell()

            if pos == 0:
                fp.write('<meta http-equiv="content-type" content="text/html; charset=UTF-8">')

            fp.close()
    except Exception,e:
        print e.message

for parent,dirnames,filenames in os.walk(rootdir):
    for filename in filenames:
        fullfilename = os.path.join(parent,filename)
        ext = fullfilename[-3:]
        if ext == '.md':
            # print fullfilename
            mdtohtml(fullfilename)


这个可以输入要转的目录,会把里面的所有.md结尾的文件都转成HTML文件,并在HTML文件开头出加入编码定义。


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值