阅读文本大概需要 4.2 分钟。
这几天相信大家的朋友圈都被华为总裁任正非的发言给刷屏了。而在 Github 上有人已经把任总的思想之路全部收集起来。
这个库收录了任正非讲话稿 400 余篇,从 1994 年到 2018 年,从深圳、中国到东南亚、非洲、欧洲、美洲,从研发、市场、服务到财经、人力资源、战略、内控与公共关系,从交换机、通讯设备、移动终端到人工智能、物联网,从 2G、3G 到 4G、5G,从物理学、化学、数学到心理学、哲学,从……到……,穿越时空看华为大幕如何徐徐展开,观任正非大家成长进化,向优秀学习,与时代共舞。
任总的思想之路每篇都非常的震撼,都有大量的信息可以值得我们学习。但是 Github 上的文件是 md 格式看起来非常的不方便,于是乎我写了个 Python 代码把 md 的格式转化成 pdf 合集,最后呈现了这样的效果。
全网最全任正非思想合集(高清 PDF)
我已经把任总的思想全部整理成一个 pdf 合集,你可以像看书一样把任总这些年所有的精华全部学习一遍。
后台回复「任正非」即可获取这个 pdf 合集。
当然做为技术博主,我也把程序的思路分享给大家。
程序思路
要实现把 md 格式的文件转换成 pdf,需要用到以下两个库。
markdown
-
wkhtmltopdf
直接把 md 转换成 pdf 的效果是非常不好的,所以我们可以先把 md 文件转换成 html 文件,然后在把 html 文件转换成 pdf。这样整体的效果就非常的好。
而上方的两个库就对应着把 md 转成 html,把 html 转成 pdf。所以我们首先要把这两个库进行安装。
1 md2html
为了将 md 格式转换成 html 文件,我们需要用到 markdown 和 codecs 这两个库。
1.1 Python-Markdown
Python-Markdown 是John Gruber 的Markdown的Python 实现。利用这个库我们就可以很容易把 md 格式的文件转换成 html 文件。
安装方式「pip install markdown」
随后我们使用 markdown.markdown() 函数就可以读取 md 文件里的内容了。
1.2 codecs
在 md2html 函数中还用到了 codecs 这个库,这个库是用来保证文件读取的过程中,不出现编码的问题。你也可以把它理解成 open 函数,用来读取创建文件用的。
1.3 完整代码
import markdown
import os
import codecs
'''
savepath = "F:\RenZhengfei-master/1996"
os.chdir(savepath)
file = codecs.open("README.md", mode="r", encoding="utf-8")
text = file.read()
html = markdown.markdown(text)
print(html)
with open('file_name.html', 'w') as f:
f.write(html)
'''
head = """<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.05);
}
</style>
</head>
<body>
"""
foot = """
</body>
</html>
"""
filepath = "F:\RenZhengfei-master\ALL"
savepath = "F:\RenZhengfei-master\ALL-html"
if not os.path.isdir(savepath):
os.mkdir(savepath)
os.chdir(savepath)
i = 0
pathDir = os.listdir(filepath)
for allDir in pathDir:
if (allDir == "pdf"):
continue
name = allDir
print(name)
os.chdir(filepath)
fp1 = codecs.open(name, mode="r", encoding="utf-8")
text = fp1.read()
html = markdown.markdown(text)
fp1.close()
#print(html)
fname = name.replace('md', 'html')
#f2 = '%s.html' % (fname)
os.chdir(savepath)
fp2 = codecs.open(fname, "w", encoding="utf-8", errors="xmlcharrefreplace")
fp2.write(head + html + foot)
fp2.close()
print(i)
整个代码还是比较简单,利用 markdown 读取 md 文件,然后在用 codeces 创建新的 html 文件。最后在开头定义好 html 头部和尾部的 css 代码,中间的内容用读取到的 md 内容进行填充。
2 html2pdf
2.1 wkhtmltopdf
wkhtmltopdf 是一个开源、简单而有效的命令行 shell 程序,它可以将任何 HTML (网页)转换为 PDF 文档或图像(jpg、png 等)。
我们首先需要去官网去下载对应的程序到本地环境中。
https://wkhtmltopdf.org/downloads.html
而在 Python 中我们要通过 pdfkit 这个库来调用 wkhtmltopdf,所以我们还要安装下 pdfkit 库。
直接使用「pip install pdfkit」即可安装。
2.2 完整代码
import time
import pdfkit
import os
wk_path = r'E:\Program Files\wkhtmltox\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=wk_path)
filepath = "F:\RenZhengfei-master\ALL-html"
savepath = "F:\RenZhengfei-master\ALL-pdf"
time1 = time.time()
pathDir = os.listdir(filepath)
for allDir in pathDir:
if (allDir == "pdf"):
continue
name = allDir
print(name)
htmlpath=filepath+"\\"+name
print(htmlpath)
name = name.replace('html', 'pdf')
os.chdir(savepath)
pdfkit.from_url(htmlpath, name, configuration=config)
#pdfkit.from_url(url, name, configuration=config)
time2 = time.time()
print(str(time2 - time1)+" s")
整个程序非常的简单这,里把 wk_path 修改成你自己的 wkhtmltopdf 安装路径。file_path 是你的 html 文件路径,而 save_path 就是你 pdf 文件保存的地方。
总结
整个过程用到了两个函数文件,文章已经把所有的源码全部贴了出来,大家复制到本地就可以使用了,记得把相应的库安装好。
为了力挺华为,我不仅把任总的思想之路整理成一个 PDF 合集,还连夜制作了一张海报。想要 PDF 合集的同学,可以扫描下方二维码,回复「任正非」即可获取。
人必有痴,而后有成