0
收藏
微博
微信
复制链接

Python技术干货:文本转PDF和生成二维码

2022-05-19 10:55
1641

随着人工智能的兴起,Python语言的优势日渐凸显,因Python具有很大的发展潜力,人们对它的需求也开始水涨船高。镜头小编将分享Python两个鲜为人知的实用操作,即文本转PDF和生成二维码,希望对小伙伴们有所帮助,如果可以能给我点个小心心嘛。
1、文本转PDF
PDF文件相对于其它形式如txt、word等文本,最大特点是无需考虑平台或设备,不会丢失数据,常被应用制作电子书。那么如果我们有文本文件,该如何把它转化成PDF文件?这就要用到Python的fpdf包。
安装fpdf包:
pip install fpdf
文本转PDF:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page() # Add a page
pdf.set_font("Arial", size = 15) # set style and size of font
f = open("game_notes.txt", "r") # open the text file in read mode
# insert the texts in pdf
for x in f:
pdf.cell(50,5, txt = x, ln = 1, align = 'C')
#pdf.output("path where you want to store pdf file\\file_name.pdf")
pdf.output("game_notes.pdf")
2、生成二维码
在日常生活中我们常用到二维码,如扫码支付、核酸检测等,二维码因其安全性高,高可靠性和节省时间被广泛应用,但你有没有想过,我们也可以通过二维码来为网站或个人资料进行加密?
生成二维码需要用上Python的qrcode库,安装方法如下:
pip install qrcode
生成二维码:
#import the library
import qrcode
#link to the website
input_data = "https://car-price-prediction-project.herokuapp.com/%22
#Creating object
#version: defines size of image from integer(1 to 40), box_size = size of each box in pixels, border = thickness of the border.
qr = qrcode.QRCode(version=1,box_size=10,border=5)
#add_date : pass the input text
qr.add_data(input_data)
#converting into image
qr.make(fit=True)
#specify the foreground and background color for the img
img = qr.make_image(fill='black', back_color='white')
#store the image
img.save('qrcode_img.png')

登录后查看更多
0
评论 0
收藏
侵权举报
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表凡亿课堂立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。

热门评论0

相关文章

电路之家

专注电子、科技分享,对电子领域深入剖解

开班信息