python使用reportlab将目录下所有的文本文件打印成pdf的方法

本文实例讲述了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

Posted in 未分类

发表评论