Python Forum
Using this Python version of text2pdf
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using this Python version of text2pdf
#1
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
pedro@pedro-newssd:~/getEmailDataSummer2019/18BE/text2pdf$


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)
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  run part of a script with a different version of Python jdog 2 443 Jan-09-2024, 08:49 PM
Last Post: jdog
  How to find out from outside Python (in Windows) the current version of Python? pstein 4 728 Oct-04-2023, 10:01 AM
Last Post: snippsat
  How to resolve version conflicts in Python? taeefnajib 0 907 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  Python venv and PIP version issue JanOlvegg 2 1,252 Feb-22-2023, 02:22 AM
Last Post: JanOlvegg
  Python Version upgrade nitinkukreja 1 894 Feb-04-2023, 10:27 PM
Last Post: Larz60+
  Can't update new python version on Pycharm GOKUUUU 6 3,790 Jul-23-2022, 09:24 PM
Last Post: GOKUUUU
  Building python (3.9.5) with different libexpat version (2.4.6) raghupcr 0 1,304 Feb-25-2022, 11:29 AM
Last Post: raghupcr
  Python keeps running the old version of the code quest 2 3,748 Jan-20-2022, 07:34 AM
Last Post: ThiefOfTime
  db migration(db version control) for python lubaz 2 2,755 May-30-2021, 01:36 PM
Last Post: lubaz
  Error on Python Version? ErnestTBass 6 3,475 Dec-09-2020, 04:02 PM
Last Post: ErnestTBass

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020