今天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文件开头出加入编码定义。