Using this Python version of text2pdf - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using this Python version of text2pdf (/thread-17482.html) |
Using this Python version of text2pdf - Pedroski55 - Apr-13-2019 I'm trying to convert text files to pdf. I want to set the page to A5, landscape. To my surprise, I found this Python file in /home/pedro/.local/bin/ I ran this Python in a bash shell with absolute paths. I thought maybe there is another version of text2pdf somewhere, because some of my changes don't seem to work. Quote:pedro@pedro-newssd:~/getEmailDataSummer2019/18BE/text2pdf$ for i in /home/pedro/getEmailDataSummer2019/18BE/text2pdf/*.txt; do /home/pedro/.local/bin/text2pdf -o "$i".pdf "$i"; done It works fine after I set the linesize in line 49 manually to 130. I changed line 10 to size = 8 I changed line 24 (it was orientation='portrait') to def make_pdf(afile, orientation='landscape', encoding='utf-8', point=(30, 20)): so I would have landscape orientation, but I still get letter or A4, portrait. I changed line 42. (It was A4) to: printer.setPageSize(Qp.QPrinter.A5)I still get A4 or letter, (not sure exactly) portrait. Does anyone know why the page format does not change?? #!/usr/bin/python3 import os.path import argparse import PyQt5.QtWidgets as Qw import PyQt5.QtGui as Qg import PyQt5.QtPrintSupport as Qp def best_font_size(linesize, pageRect, fontname, margin=5): size = 8 while True: font = Qg.QFont(fontname, size) font_line_height = Qg.QFontMetrics(font).height() font_line_width = (Qg.QFontMetrics(font).width('A') * linesize) #print(int(pageRect.width() / Qg.QFontMetrics(font).width('A'))) #print(int(pageRect.height() / Qg.QFontMetrics(font).height())) if pageRect.width() < font_line_width: size = size - 1 else: break return size def make_pdf(afile, orientation='landscape', encoding='utf-8', point=(30, 20)): """Create PDF from text :param afile: Text file to convert :param orientation: PDF orientation (lamdscape or portrait) :param encoding: The actual encoding of afile :param point: (column, row) start point """ # fontname = "DejaVu Sans Mono" # fontname = "Monospace" fontname = "Ubuntu Mono" # fontname = "Liberation Mono" start_row, start_col = point with open(afile, encoding=encoding) as afi: lines = list(afi) *fname, _ = afile.split('.') fname = '.'.join(fname + ['pdf']) printer = Qp.QPrinter() printer.setPageSize(Qp.QPrinter.A5) printer.setOutputFileName(fname) printer.setOutputFormat(Qp.QPrinter.PdfFormat) if orientation == 'landscape': printer.setOrientation(Qp.QPrinter.Landscape) pageRect = printer.pageRect() #bestSize = best_font_size( # max([len(i) for i in lines]), pageRect, fontname) bestSize = best_font_size( 130, pageRect, fontname) font = Qg.QFont(fontname, bestSize) # font.setStyleHint(Qg.QFont.TypeWriter) font_line_height = Qg.QFontMetrics(font).height() painter = Qg.QPainter(printer) page = 1 row = start_row for line in lines: painter.save() painter.setFont(font) row += font_line_height try: painter.drawText(start_col, row, line) except: painter.drawText(start_col, row, 'CodePage error !!!') if row > (pageRect.height() - 54): printer.newPage() row = start_row painter.restore() painter.end() if __name__ == '__main__': pars = argparse.ArgumentParser(description='Convert text to pdf') pars.add_argument('file', help='Text FILE to be converted') pars.add_argument('-e', '--Encoding', help='Encoding of file') pars.add_argument('-o', '--Orientation', help='portrait or landscape') pars.add_argument('--version', action='version', version='0.1') args = pars.parse_args() if not os.path.isfile(args.file): print('No such file : %s' % args.file) else: app = Qw.QApplication([]) ret = make_pdf(args.file, args.Orientation, args.Encoding) RE: Using this Python version of text2pdf - Pedroski55 - Apr-15-2019 To get good control over pdfs, use pandoc. pedro@pedro-newssd:~/clozetexts$ pandoc -V=geometry:a5paper -V geometry:margin=0.5in -V geometry:landscape ILSU1GreenDesignHV1.txt -o ILSU1GreenDesignHV1.pdf |