Python實戰(zhàn)之markdown轉(zhuǎn)pdf(包含公式轉(zhuǎn)換)
一、Pandoc轉(zhuǎn)換
1.1 問題
由于我們markdown編輯器比較特殊,一般情況下,我們不太好看,如果轉(zhuǎn)換成pdf的話,我們就不需要可以的去安裝各種編輯器才可以看了,所以我們有了md轉(zhuǎn)pdf或者是docx的需求。
1.2 下載
安裝后,本地查看版本,是否安裝成功:

出現(xiàn)如上圖表示安裝成功。
1.3 md轉(zhuǎn)docx
cd進入我們需要轉(zhuǎn)換的文件目錄下,輸入:
pandoc xxx.md -s -o xxxx.docx
-s:生成恰當(dāng)?shù)奈募^部和底部。
-o:指定輸出的文件。
查看實際效果:


此時發(fā)現(xiàn)文件已經(jīng)生成好.我們打開看下,

整體轉(zhuǎn)換效果還是不錯的。
1.4 md轉(zhuǎn)pdf
pandoc xxx.md -o xxxx.pdf --pdf-engine=xelatex
二、python庫實現(xiàn)
使用 Typora可以直接轉(zhuǎn)換
結(jié)合 wkhtmltopdf 使用 markdown 庫 和 pdfkit 庫
2.1 安裝 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轉(zhuǎn)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轉(zhuǎn)PDF
但是我們此時存在一個問題,如果我們的md中有表格的話,如圖:

那么轉(zhuǎn)換之后會發(fā)現(xiàn)是亂的:

我們此時需要設(shè)定參數(shù),修改為如下:
html = markdown(text, output_format='html',extensions=['tables'])
我們再看下效果:

2.3 引入數(shù)學(xué)公式
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" 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轉(zhuǎn)HTML
html = html.format(text)
pdfkit.from_string(html, output_filename, options={'encoding': 'utf-8'}) # HTML轉(zhuǎn)PDF
2.4 網(wǎng)頁轉(zhuǎn)pdf
import pdfkit
pdfkit.from_file('xxx.html', 'xxxx.pdf', options={'encoding': 'utf-8'}) # HTML轉(zhuǎn)PDF
2.5 進度條轉(zhuǎn)換
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轉(zhuǎn)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轉(zhuǎn)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}
我們看下最后的實際效果:

到此這篇關(guān)于Python實戰(zhàn)之markdown轉(zhuǎn)pdf(包含公式轉(zhuǎn)換)的文章就介紹到這了,更多相關(guān)Python markdown轉(zhuǎn)pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python模塊Typing.overload的使用場景分析
在 Python 中,typing.overload 是一個用于定義函數(shù)重載的裝飾器,函數(shù)重載是指在一個類中可以定義多個相同名字但參數(shù)不同的函數(shù),使得在調(diào)用函數(shù)時可以根據(jù)參數(shù)的不同選擇不同的函數(shù)執(zhí)行,這篇文章主要介紹了Python模塊Typing.overload的使用,需要的朋友可以參考下2024-02-02
關(guān)于Python字符編碼與二進制不得不說的一些事
這篇文章主要給大家介紹了關(guān)于Python字符編碼與二進制不得不說的一些事,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
python中文件的創(chuàng)建與寫入實戰(zhàn)代碼
這篇文章主要給大家介紹了關(guān)于python中文件的創(chuàng)建與寫入的相關(guān)資料,在Python中文件寫入提供了不同的模式和方法來滿足不同的需求,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
Python猜解網(wǎng)站數(shù)據(jù)庫管理員密碼的腳本
這篇文章主要和大家分享一個Python腳本,可以實現(xiàn)猜解網(wǎng)站數(shù)據(jù)庫管理員的密碼。文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2022-02-02
PyCharm:method may be static問題及解決
這篇文章主要介紹了PyCharm:method may be static問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

