金狮镖局 Design By www.egabc.com

笔者在今天的工作中,遇到了一个需求,那就是如何将Python字符串生成PDF。比如,需要把Python字符串‘这是测试文件'生成为PDF, 该PDF中含有文字‘这是测试文件'。

"external nofollow" href="https://wkhtmltopdf.org/downloads.html">https://wkhtmltopdf.org/downloads.html ,读者可根据自己的系统下载对应的文件并安装。安装好wkhtmltopdf,我们再安装这个软件的Python第三方模块——pdfkit,安装方式如下:

pip install pdfkit

"color: #ff0000">如何将Python字符串生成PDF

"htmlcode">

import pdfkit
# PDF中包含的文字
content = '这是一个测试文件。' + '<br>' + 'Hello from Python!'
html = '<html><head><meta charset="UTF-8"></head>'  '<body><div align="center"><p>%s</p></div></body></html>'%content
# 转换为PDF
pdfkit.from_string(html, './test.pdf')

输出的结果如下:

Loading pages (1/6)
 Counting pages (2/6)
 Resolving links (4/6)
 Loading headers and footers (5/6)
 Printing pages (6/6)
 Done

生成的test.pdf如下:

将Python字符串生成PDF的实例代码详解

如何生成PDF中的表格

"text-align: center">"//img.jbzj.com/file_images/article/201905/201905171431522.png" style="font-size: medium; font-family: "Microsoft YaHei"; white-space: normal; text-transform: none; word-spacing: 0px; font-weight: 400; color: rgb(0,0,0); font-style: normal; orphans: 2; widows: 2; letter-spacing: normal; text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial" alt="" />

"htmlcode">

import pdfkit
# 读取csv文件
with open('iris.csv', 'r') as f:
 lines = [_.strip() for _ in f.readlines()]
# 转化为html中的表格样式
td_width = 100
content = '<table width="%s" border="1" cellspacing="0px" style="border-collapse:collapse">' % (td_width*len(lines[0].split(',')))
for i in range(len(lines)):
 tr = '<tr>'+''.join(['<td width="%d">%s</td>'%(td_width, _) for _ in lines[i].split(',')])+'</tr>'
 content += tr
content += '</table>'
html = '<html><head><meta charset="UTF-8"></head>'  '<body><div align="center">%s</div></body></html>' % content
# 转换为PDF
pdfkit.from_string(html, './iris.pdf')

"text-align: center">将Python字符串生成PDF的实例代码详解

解决PDF生成速度慢的问题

"htmlcode">

import pdfkit
import time
start_time = time.time()
for i in range(100):
 content = '这是第%d份测试文件!'%(i+1)
 html = '<html><head><meta charset="UTF-8"></head>'   '<body><div align="center">%s</div></body></html>' % content
 # 转换为PDF
 pdfkit.from_string(html, './test/%s.pdf'%(i+1))
end_time = time.time()
print('一共耗时:%s 秒.' %(end_time-start_time))

在这个程序中,生成100份PDF文件一共耗时约192秒。输出结果如下:

......
Loading pages (1/6)
Counting pages (2/6)                                              
Resolving links (4/6)                                                      
Loading headers and footers (5/6)                                          
Printing pages (6/6)
Done                                                                     
一共耗时:191.9226369857788 秒.

"htmlcode">

import pdfkit
import time
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
start_time = time.time()
# 函数: 生成PDF
def convert_2_pdf(i):
 content = '这是第%d份测试文件!'%(i+1)
 html = '<html><head><meta charset="UTF-8"></head>'   '<body><div align="center">%s</div></body></html>' % content
 # 转换为PDF
 pdfkit.from_string(html, './test/%s.pdf'%(i+1))
# 利用多线程生成PDF
executor = ThreadPoolExecutor(max_workers=10) # 可以自己调整max_workers,即线程的个数
# submit()的参数: 第一个为函数, 之后为该函数的传入参数,允许有多个
future_tasks = [executor.submit(convert_2_pdf, i) for i in range(100)]
# 等待所有的线程完成,才进入后续的执行
wait(future_tasks, return_when=ALL_COMPLETED)
end_time = time.time()
print('一共耗时:%s 秒.' %(end_time-start_time))

在这个程序中,生成100份PDF文件一共耗时约41秒,明显快了很多~

总结

以上所述是小编给大家介绍的将Python字符串生成PDF的相关知识,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

标签:
python字符串生成PDF,python字符串pdf

金狮镖局 Design By www.egabc.com
金狮镖局 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com

评论“将Python字符串生成PDF的实例代码详解”

暂无将Python字符串生成PDF的实例代码详解的评论...