This example describes how Python uses Reportlab to print all text files in a directory to PDF. Share to everyone for your reference. The implementation method is as follows:
#-*-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 ', ten) Textobject = C.begintext () textobject.settextorigin (inch,11*inch) File=open (FileName) n=0 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
Hopefully this article will help you with Python programming.