Python实战之markdown转pdf(包含公式转换)
作者:指尖听戏 发布时间:2023-11-24 12:39:34
标签:Python,markdown,pdf
一、Pandoc转换
1.1 问题
由于我们markdown编辑器比较特殊,一般情况下,我们不太好看,如果转换成pdf的话,我们就不需要可以的去安装各种编辑器才可以看了,所以我们有了md转pdf或者是docx的需求。
1.2 下载
资源地址
安装后,本地查看版本,是否安装成功:
出现如上图表示安装成功。
1.3 md转docx
cd进入我们需要转换的文件目录下,输入:
pandoc xxx.md -s -o xxxx.docx
-s:生成恰当的文件头部和底部。
-o:指定输出的文件。
查看实际效果:
此时发现文件已经生成好.我们打开看下,
整体转换效果还是不错的。
1.4 md转pdf
pandoc xxx.md -o xxxx.pdf --pdf-engine=xelatex
二、python库实现
使用 Typora可以直接转换
结合 wkhtmltopdf 使用 markdown 库 和 pdfkit 库
2.1 安装 wkhtmltopdf
wkhtmltopdf 下载地址
2.2 安装 mdutils
pip install markdown
pip install pdfkit
参考案例:
import pdfkit
from markdown import markdown
input = r"F:\csdn博客\pytorch\【Pytorch】pytorch安装.md"
output = r"【Pytorch】pytorch安装.pdf"
with open(input, encoding='utf-8') as f:
text = f.read()
html = markdown(text, output_format='html') # MarkDown转HTML
htmltopdf = r'D:\htmltopdf\wkhtmltopdf\bin\wkhtmltopdf.exe'
configuration = pdfkit.configuration(wkhtmltopdf=htmltopdf)
pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8'}) # HTML转PDF
但是我们此时存在一个问题,如果我们的md中有表格的话,如图:
那么转换之后会发现是乱的:
我们此时需要设定参数,修改为如下:
html = markdown(text, output_format='html',extensions=['tables'])
我们再看下效果:
2.3 引入数学公式
pip install python-markdown-math
import pdfkit
from markdown import markdown
input_filename = 'xxxx.md'
output_filename = 'xxxx.pdf'
html = '<!DOCTYPE html><body><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex/dist/katex.min.css" rel="external nofollow" crossorigin="anonymous"><script src="https://cdn.jsdelivr.net/npm/katex/dist/katex.min.js" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/katex/dist/contrib/mathtex-script-type.min.js" defer></script>{}</body></html>'
text = '$$E=mc^2$$'
text = markdown(text, output_format='html', extensions=['mdx_math']) # MarkDown转HTML
html = html.format(text)
pdfkit.from_string(html, output_filename, options={'encoding': 'utf-8'}) # HTML转PDF
2.4 网页转pdf
import pdfkit
pdfkit.from_file('xxx.html', 'xxxx.pdf', options={'encoding': 'utf-8'}) # HTML转PDF
2.5 进度条转换
pip install pymdown-extensions
progressbar.css
.progress-label {
position: absolute;
text-align: center;
font-weight: 700;
width: 100%;
margin: 0;
line-height: 1.2rem;
white-space: nowrap;
overflow: hidden;
}
.progress-bar {
height: 1.2rem;
float: left;
background-color: #2979ff;
}
.progress {
display: block;
width: 100%;
margin: 0.5rem 0;
height: 1.2rem;
background-color: #eeeeee;
position: relative;
}
.progress.thin {
margin-top: 0.9rem;
height: 0.4rem;
}
.progress.thin .progress-label {
margin-top: -0.4rem;
}
.progress.thin .progress-bar {
height: 0.4rem;
}
.progress-100plus .progress-bar {
background-color: #00e676;
}
.progress-80plus .progress-bar {
background-color: #fbc02d;
}
.progress-60plus .progress-bar {
background-color: #ff9100;
}
.progress-40plus .progress-bar {
background-color: #ff5252;
}
.progress-20plus .progress-bar {
background-color: #ff1744;
}
.progress-0plus .progress-bar {
background-color: #f50057;
}
progressbar.py
from markdown import markdown
filename = 'progressbar.md'
html = '''
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">
<title>progressbar</title>
<link rel="stylesheet" href="progressbar.css" rel="external nofollow" >
</head>
<body>
{}
</body>
</html>
'''
encoding = 'utf-8'
with open(filename, encoding=encoding) as f:
text = f.read()
extensions = [
'markdown.extensions.attr_list',
'pymdownx.progressbar'
]
text = markdown(text, output_format='html', extensions=extensions) # MarkDown转HTML
html = html.format(text)
print(html)
with open(filename.replace('.md', '.html'), 'w', encoding=encoding) as f:
f.write(html)
# pdfkit.from_string(html, output, options={'encoding': 'utf-8'}) # HTML转PDF
print('完成')
progressbar.md
[=0% "0%"]
[=5% "5%"]
[=25% "25%"]
[=45% "45%"]
[=65% "65%"]
[=85% "85%"]
[=100% "100%"]
[=85% "85%"]{: .candystripe}
[=100% "100%"]{: .candystripe .candystripe-animate}
[=0%]{: .thin}
[=5%]{: .thin}
[=25%]{: .thin}
[=45%]{: .thin}
[=65%]{: .thin}
[=85%]{: .thin}
[=100%]{: .thin}
我们看下最后的实际效果:
来源:https://blog.csdn.net/qq_38140292/article/details/121511178


猜你喜欢
- 某位 A 同学发了我一张截图,问为何结果中出现了负数?看了图,我第一感觉就是数据溢出了。数据超出能表示的最大值,就会出现奇奇怪怪的结果。然后
- 导语哈喽铁汁们好久不见吖~小编已经复工了于是马不停蹄赶来给大家准备新年礼物算开工礼物吧!海龟来作图虎年就是要画老虎2022不用纸和笔~今晚画
- 这篇文章主要介绍了JavaScript回调函数callback用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 想通过编写Python代码来打开本地的.mp4格式文件,使用os模块来操作文件。我的电脑默认的是QQ影音播放器,执行Python代码打开默认
- 之前,我们在另外一篇文章中使用Prim算法生成了一个完美迷宫,利用的是遍历网格的方法,这一次,我们要教教大家用遍历墙的方法生成,上一篇文章链
- 如果和不同的后台调接口,如果后台接口没有合到一起,前端可以配不同的代理来共同访问他们的接口在config文件夹下的index.js中设置如下
- 本文实例讲述了Python单元测试方法。分享给大家供大家参考,具体如下:Eric书中《Python编程从入门到实践》中的一个例子。《Pyth
- 例子一:Python用WMI模块获取windowns系统的硬件信息:硬盘分区、使用情况,内存大小,CPU型号,当前运行的进程,自启动程序及位
- 关于缓存剩下的问题是数据的隐私性以及在级联缓存中数据应该在何处储存的问题。通常用户将会面对两种缓存: 他或她自己的浏览器缓存(私有缓存)以及
- 引言日常开发中,我们经常会使用到group by。亲爱的小伙伴,你是否知道group by的工作原理呢?group by和having有什么
- 基本函数如下:/// <summary> /// 需要分页时使用,根据参数和ConditionExpress获取DataTabl
- 1. mysql的md5 mysql存在系统函数md5(“xxxxx”);2. mssql的md5&nb
- 问题:根据数据某列进行分组,选择其中另一列大小top-K的的所在行数据解析:求解思路很清晰,即先用groupby对数据进行分组,然后再根据分
- 引言“深入认识Python内建类型”这部分的内容会从源码角度为大家介绍Python中各种常用的内建类
- 使用JS对Json数据的处理,项目遇到需要对Json数据进行相关操作,比如增删改操作,本以为会比较难,网上搜索下,发现还是比较简单的,贴一段
- 对象Javascript 根本上是和对象相关的。数组是对象。函数是对象。对象是对象。那什么是对象呢?对象是名-值对的集合。名是字符串,值可以
- 纪念我的第一个爬虫程序,一共写了三个白天,其中有两个上午没有看,中途遇到了各种奇怪的问题,伴随着他们的解决,对于一些基本的操作也弄清楚了。果
- 不知道有没有人碰到过这样恶心的问题:两张表连接查询并limit,SQL效率很高,但是加上order by以后,语句的执行时间变的巨长,效率巨
- 在操作DataFrame时,肯定会经常用到loc,iloc,at等函数,各个函数看起来差不多,但是还是有很多区别的,我们一起来看下吧。首先,
- 本文代码将一些简单常用的SQL语句,拆分、封装成链式函数与终结函数,链式操作没有先后之分,实现傻瓜式mysql数据库操作。 同时学习下静态成