Welcome, guest | Sign In | My Account | Store | Cart

This recipe shows how to do a batch conversion of the content of multiple text files into a single PDF file, with a) an automatic page break after the content of each text file (in the PDF output), b) page numbering, and c) a header and footer on each page.

It uses the fileinput module (part of the Python standard library), and xtopdf, a Python library for conversion of other formats to PDF.

xtopdf is available here: https://bitbucket.org/vasudevram/xtopdf

and a guide to installing and using xtopdf is here:

http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html

Here is a sample run of the program:

python BTTP123.pdf text1.txt text2.txt text3.txt

This will read the content from the three text files specified and write it into the PDF file specified, neatly formatted.

Python, 47 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from __future__ import print_function

# BatchTextToPDF.py
# Convert a batch of text files to a single PDF.
# Each text file's content starts on a new page in the PDF file.
# Requires:
# - xtopdf: https://bitbucket.org/vasudevram/xtopdf
# - ReportLab: https://www.reportlab.com/ftp/reportlab-1.21.1.tar.gz
# Author: Vasudev Ram
# Copyright 2016 Vasudev Ram
# Product store: https://gumroad.com/vasudevram
# Web site: https://vasudevram.github.io
# Blog: http://jugad2.blogspot.com

import sys
import fileinput
from PDFWriter import PDFWriter

def usage(prog_name):
    sys.stderr.write("Usage: {} outfile.pdf infile1.txt ...".format(prog_name))

def main():

    if len(sys.argv) < 3:
        usage(sys.argv[0])
        sys.exit(0)

    try:
        pw = PDFWriter(sys.argv[1])
        pw.setFont('Courier', 12)
        pw.setFooter('xtopdf: https://google.com/search?q=xtopdf')

        for line in fileinput.input(sys.argv[2:]):
            if fileinput.filelineno() == 1:
                pw.setHeader(fileinput.filename())
                if fileinput.lineno() != 1:
                    pw.savePage()
            pw.writeLine(line.strip('\n'))

        pw.savePage()
        pw.close()
    except Exception as e:
        print("Caught Exception: type: {}, message: {}".format(\
            e.__class__, str(e)))

if __name__ == '__main__':
    main()

Here is a good overview of xtopdf, the PDF generation library used in the recipe:

http://slides.com/vasudevram/xtopdf

It has information on supported platforms and input formats, uses, users, etc., of xtopdf.

1 comment

Vasudev Ram (author) 7 years, 5 months ago  # | flag