最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實戰(zhàn)之markdown轉(zhuǎn)pdf(包含公式轉(zhuǎn)換)

 更新時間:2021年12月15日 11:41:17   作者:指尖聽?wèi)? 
由于我們markdown編輯器比較特殊,不是很方便瀏覽,如果轉(zhuǎn)換成pdf的話,就不需要可以的去安裝各種編輯器才可以看了。所以本文將介紹如何通過Python實現(xiàn)md轉(zhuǎn)pdf或者是docx,需要的朋友可以參考一下

一、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進(jìn)入我們需要轉(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

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 進(jìn)度條轉(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)文章

最新評論

南充市| 越西县| 南岸区| 永德县| 台山市| 上林县| 宣威市| 临漳县| 休宁县| 安岳县| 高清| 宜丰县| 辽阳县| 清丰县| 芒康县| 井冈山市| 富顺县| 新干县| 泰来县| 佛教| 文山县| 卢龙县| 德庆县| 甘南县| 岳阳县| 托克逊县| 榆中县| 墨玉县| 德昌县| 东莞市| 高清| 鹤壁市| 泸西县| 峡江县| 瓦房店市| 日喀则市| 伊春市| 二手房| 姚安县| 永平县| 大余县|