时间:2021-07-01 10:21:17 帮助过:42人阅读
# -*- 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<10): nstr='0'+str(n) else: nstr=str(n) line=nstr+': '+line.replace(' ',' ') textobject.textLine(line.rstrip()) c.drawText(textobject) c.showPage() c.save() # FileList=wlab.GetFileList('.',FlagStr=['.txt']) # for FileName in FileList: file2pdf(FileName)
希望本文所述对大家的Python程序设计有所帮助。