Python實(shí)戰(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進(jìn)入我們需要轉(zhuǎn)換的文件目錄下,輸入:
pandoc xxx.md -s -o xxxx.docx
-s:生成恰當(dāng)?shù)奈募^部和底部。
-o:指定輸出的文件。
查看實(shí)際效果:
此時(shí)發(fā)現(xiàn)文件已經(jīng)生成好.我們打開看下,
整體轉(zhuǎn)換效果還是不錯(cuò)的。
1.4 md轉(zhuǎn)pdf
pandoc xxx.md -o xxxx.pdf --pdf-engine=xelatex
二、python庫實(shí)現(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
但是我們此時(shí)存在一個(gè)問題,如果我們的md中有表格的話,如圖:
那么轉(zhuǎn)換之后會(huì)發(fā)現(xiàn)是亂的:
我們此時(shí)需要設(shè)定參數(shù),修改為如下:
html = markdown(text, output_format='html',extensions=['tables'])
我們?cè)倏聪滦Ч?/span>
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}
我們看下最后的實(shí)際效果:
到此這篇關(guān)于Python實(shí)戰(zhàn)之markdown轉(zhuǎn)pdf(包含公式轉(zhuǎn)換)的文章就介紹到這了,更多相關(guān)Python markdown轉(zhuǎn)pdf內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python模塊Typing.overload的使用場(chǎng)景分析
在 Python 中,typing.overload 是一個(gè)用于定義函數(shù)重載的裝飾器,函數(shù)重載是指在一個(gè)類中可以定義多個(gè)相同名字但參數(shù)不同的函數(shù),使得在調(diào)用函數(shù)時(shí)可以根據(jù)參數(shù)的不同選擇不同的函數(shù)執(zhí)行,這篇文章主要介紹了Python模塊Typing.overload的使用,需要的朋友可以參考下2024-02-02關(guān)于Python字符編碼與二進(jìn)制不得不說的一些事
這篇文章主要給大家介紹了關(guān)于Python字符編碼與二進(jìn)制不得不說的一些事,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10python中文件的創(chuàng)建與寫入實(shí)戰(zhàn)代碼
這篇文章主要給大家介紹了關(guān)于python中文件的創(chuàng)建與寫入的相關(guān)資料,在Python中文件寫入提供了不同的模式和方法來滿足不同的需求,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Python猜解網(wǎng)站數(shù)據(jù)庫管理員密碼的腳本
這篇文章主要和大家分享一個(gè)Python腳本,可以實(shí)現(xiàn)猜解網(wǎng)站數(shù)據(jù)庫管理員的密碼。文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2022-02-02PyCharm:method may be static問題及解決
這篇文章主要介紹了PyCharm:method may be static問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的MySQL類
這篇文章主要介紹了Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的MySQL類,可實(shí)現(xiàn)基本的初始化連接及查詢、刪除等功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01python取數(shù)作為臨時(shí)極大值(極小值)的方法
今天小編就為大家分享一篇python取數(shù)作為臨時(shí)極大值(極小值)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10