Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Using this Python version of text2pdf
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:

I still get A4 or letter, (not sure exactly) portrait.

Does anyone know why the page format does not change??

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
    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()
    if orientation == '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:
        row += font_line_height
            painter.drawText(start_col, row, line)
            painter.drawText(start_col, row, 'CodePage error !!!')
        if row > (pageRect.height() - 54):
            row = start_row

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)
        app = Qw.QApplication([])
        ret = make_pdf(args.file, args.Orientation, args.Encoding)
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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Version of glob for that Supports Windows Wildcards? Reverend_Jim 4 303 Jun-16-2019, 04:52 AM
Last Post: DeaD_EyE
  [split] Using this Python version of text2pdf jonsnow1221 1 97 May-07-2019, 07:48 PM
Last Post: micseydel
  can't install package web in version python 3.7 a21250450 1 135 Mar-29-2019, 11:28 AM
Last Post: Larz60+
  Hello Im looking for a compiled window version of 3.4.x ... SlowMotion 2 164 Feb-12-2019, 08:13 PM
Last Post: SlowMotion
  Python Version on Mac OSX Sparkstorm 5 262 Feb-06-2019, 10:32 AM
Last Post: Sparkstorm
  numpynot associted with correct python version ErnestTBass 17 724 Jan-16-2019, 02:47 PM
Last Post: ErnestTBass
  Python version 3 that seems to be ignored by the system on a Raspberry Pi Discret68 3 342 Dec-28-2018, 06:42 PM
Last Post: Gribouillis
  docs for text2pdf Pedroski55 8 560 Dec-22-2018, 10:53 PM
Last Post: Pedroski55
  I'm having trouble with an OOP version of Pickle functionality CodeWolf 2 240 Dec-19-2018, 05:41 PM
Last Post: CodeWolf
  How to comapare version UUIDs in python? swamyjrs 3 269 Dec-19-2018, 08:57 AM
Last Post: Gribouillis

Forum Jump:

Users browsing this thread: 1 Guest(s)