In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to achieve markdown to pdf in Python. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!
Pandoc conversion 1.1 problem
Because our markdown editor is special, in general, we don't look good, if converted to pdf, we don't need to install various editors to see, so we have md to pdf or docx requirements.
1.2 download
resource address
After installation, check the version locally to see if the installation is successful:
Appears as shown in the figure above indicates successful installation.
1.3 md to docx
cd Enter the file directory we need to convert and enter:
pandoc xxx.md -s -o xxxx.docx
-s: Generate proper file headers and bottoms.
-o: Specifies the output file.
See the actual effect:
此时发现文件已经生成好.我们打开看下,
整体转换效果还是不错的。
1.4 md转pdfpandoc xxx.md -o xxxx.pdf --pdf-engine=xelatex二、python库实现
使用 Typora可以直接转换
结合 wkhtmltopdf 使用 markdown 库 和 pdfkit 库
2.1 安装 wkhtmltopdf
wkhtmltopdf 下载地址
2.2 安装 mdutilspip install markdownpip install pdfkit
参考案例:
import pdfkitfrom markdown import markdowninput = 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转HTMLhtmltopdf = 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-mathimport pdfkitfrom markdown import markdowninput_filename = 'xxxx.md'output_filename = 'xxxx.pdf'html = '{}'text = '$$E=mc^2$$'text = markdown(text, output_format='html', extensions=['mdx_math']) # MarkDown转HTMLhtml = html.format(text)pdfkit.from_string(html, output_filename, options={'encoding': 'utf-8'}) # HTML转PDF2.4 网页转pdfimport pdfkitpdfkit.from_file('xxx.html', 'xxxx.pdf', options={'encoding': 'utf-8'}) # HTML转PDF2.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 markdownfilename = 'progressbar.md'html = ''' progressbar {} '''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转HTMLhtml = 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转PDFprint('完成')
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}
我们看下最后的实际效果:
以上是"Python中如何实现markdown转pdf"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.