本文实例讲述了python使用reportlab将目录下所有的文本文件打印成pdf的方法。分享给大家供大家参考。具体实现方法如下:
# -*- coding: utf8 -*-
#~ #----------------------------------------------------------------------
import wlab #pip install wlab
import reportlab.pdfbase.ttfonts
#reportlab.pdfbase.pdfmetrics.registerFont(reportlab.pdfbase.ttfonts.TTFont('song', '/usr/share/fonts/cn/msjh.ttf'))
#import reportlab.lib.fonts
#
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
#
def file2pdf(FileName):
fpi=FileName.find('.')
if (fpi>0):
outputfn=FileName[0:fpi]+'.pdf'
else:
outputfn=FileName+'.pdf'
c = canvas.Canvas(outputfn)
#c.setFont('song',10)
textobject = c.beginText()
textobject.setTextOrigin(inch,11*inch)
file=open(FileName)
n=0
for line in file:
n=n+1
if(n
<p>希望本文所述对大家的Python程序设计有所帮助。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill13660" title="使用 Claude Agent SDK 将绑定的 Claude 代码会话嵌入到 Python 自动化中"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20dee630206.jpg" alt="使用 Claude Agent SDK 将绑定的 Claude 代码会话嵌入到 Python 自动化中" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill13660" title="使用 Claude Agent SDK 将绑定的 Claude 代码会话嵌入到 Python 自动化中" class="overflowclass">使用 Claude Agent SDK 将绑定的 Claude 代码会话嵌入到 Python 自动化中</a>
<p class="overflowclass">当现有的脚本或服务需要将作用域化的仓库工作委托给 Claude Code 并以编程方式消费结构化响应时,请使用 Claude Agent SDK for Python。</p>
</div>
<a rel="nofollow" href="/xiazai/skill13660" title="使用 Claude Agent SDK 将绑定的 Claude 代码会话嵌入到 Python 自动化中" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>
Python免费学习笔记(深入):立即使用
在学习笔记中,你将探索 Python 的核心概念和高级技巧!










