python如何创建pdf文件并支持中文reportlab
1、1. 参考经验如下,先安装 reportlab插件,
2、2. 通过连接下载中文字体文件 https://pan.baidu.com/s/1UHafgf1tNpcAGKJrxlfZ9Q 提取码: umay
3、3. 将中文字体文件拷贝到项目的根路径下
4、4. 在代艨位雅剖码文件中输入代码如下,注册字体def registerFont():fontPath = os.getcwd()fullPath = fontPath + "\\font\\文泉驿正黑.ttf"reportlab.pdfbase.pdfmetrics.registerFont(reportlab.pdfbase.ttfonts.TTFont('song', fullPath))
5、5. 创建代艨位雅剖码如下,该方法可以创建pdf文件def createPdf(pdfName, conte荏鱿胫协nt):c = canvas.Canvas(pdfName, pagesize=letter)# 设置字体c.setFont('song', 14)width, height = lettercontent = content.encode("utf-8")c.drawString(width / 2 - 200, height / 2 + 150, content)c.showPage()c.save()
6、6. 创建main方法,如下所示。创建完后,使用快捷键运行程序,会在工程目录路径下生成pdf文件if __name__ == '__main__':registerFont()pdfName = 'zhh1.pdf'content = "zhhh哈哈哈哈"createPdf(pdfName, content)
7、7. 如果您觉得有用,记得在下方点击投票、点赞、关注、留言,小编会定期奉上更多的惊喜哦,您的支持才是小编继续努力的动力,么么哒。