阅读 354

Python实战之markdown转pdf(包含公式转换)

由于我们markdown编辑器比较特殊,不是很方便浏览,如果转换成pdf的话,就不需要可以的去安装各种编辑器才可以看了。所以本文将介绍如何通过Python实现md转pdf或者是docx,需要的朋友可以参考一下

目录
  • 一、Pandoc转换

    • 1.1 问题

    • 1.2 下载

    • 1.3 md转docx

    • 1.4 md转pdf

  • 二、python库实现

    • 2.1 安装 wkhtmltopdf

    • 2.2 安装 mdutils

    • 2.3 引入数学公式

    • 2.4 网页转pdf

    • 2.5 进度条转换

一、Pandoc转换

1.1 问题

由于我们markdown编辑器比较特殊,一般情况下,我们不太好看,如果转换成pdf的话,我们就不需要可以的去安装各种编辑器才可以看了,所以我们有了md转pdf或者是docx的需求。

1.2 下载

资源地址

安装后,本地查看版本,是否安装成功:

出现如上图表示安装成功。

1.3 md转docx

cd进入我们需要转换的文件目录下,输入:

1
pandoc xxx.md -s -o xxxx.docx

-s:生成恰当的文件头部和底部。

-o:指定输出的文件。

查看实际效果:

此时发现文件已经生成好.我们打开看下,

整体转换效果还是不错的。

1.4 md转pdf

1
pandoc xxx.md -o xxxx.pdf --pdf-engine=xelatex

二、python库实现

使用 Typora可以直接转换

结合 wkhtmltopdf 使用 markdown 库 和 pdfkit 库

2.1 安装 wkhtmltopdf

wkhtmltopdf 下载地址


2.2 安装 mdutils

1
2
pip install markdown
pip install pdfkit

参考案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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中有表格的话,如图:

那么转换之后会发现是乱的:

我们此时需要设定参数,修改为如下:

1
html = markdown(text, output_format='html',extensions=['tables'])

我们再看下效果:

2.3 引入数学公式

1
pip install python-markdown-math
1
2
3
4
5
6
7
8
9
10
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

1
2
3
import pdfkit
 
pdfkit.from_file('xxx.html', 'xxxx.pdf', options={'encoding': 'utf-8'})  # HTML转PDF

2.5 进度条转换

1
pip install pymdown-extensions

progressbar.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
.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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[=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}

我们看下最后的实际效果:

到此这篇关于Python实战之markdown转pdf(包含公式转换)的文章就介绍到这了

原文链接:https://blog.csdn.net/qq_38140292/article/details/121511178


    文章分类
    百科问答
    版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
    相关推荐