Bottom Page

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)
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  update certbot's python version metulburr 0 87 Sep-19-2019, 11:51 AM
Last Post: metulburr
  Im using python crash course version 2 james_newbie 3 88 Sep-07-2019, 09:21 PM
Last Post: ichabod801
  [split] Using this Python version of text2pdf jonsnow1221 1 221 May-07-2019, 07:48 PM
Last Post: micseydel
  can't install package web in version python 3.7 a21250450 1 340 Mar-29-2019, 11:28 AM
Last Post: Larz60+
  Python Version on Mac OSX Sparkstorm 5 414 Feb-06-2019, 10:32 AM
Last Post: Sparkstorm
  Python version 3 that seems to be ignored by the system on a Raspberry Pi Discret68 3 501 Dec-28-2018, 06:42 PM
Last Post: Gribouillis
  docs for text2pdf Pedroski55 8 1,035 Dec-22-2018, 10:53 PM
Last Post: Pedroski55
  How to comapare version UUIDs in python? swamyjrs 3 420 Dec-19-2018, 08:57 AM
Last Post: Gribouillis
  Python version compatability saisankalpj 5 500 Nov-29-2018, 06:12 PM
Last Post: nilamo
  rpm version comparison in python asad 3 816 Jul-27-2018, 01:00 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)