Sunday, May 26, 2013

Convert multiple text files to PDF with xtopdf

This Python program, batch_text_to_pdf.py, can create a PDF file from a batch of text files.

It uses my xtopdf toolkit, ReportLab and Python.

It also uses the fileinput module from Python's standard library.

The fileinput module is quite useful for creating Unix-style command-line programs in Python, that process a batch of text files, like awk, sed, and other similar Unix tools can.

The basic pattern for these commands is:

command_name file1.txt file2.txt  ...

# batch_text_to_pdf.py

import string
import fileinput
from PDFWriter import PDFWriter

def main():

    # Hard-coded for now,
    # should take PDF name from
    # command line, by skip-
    # ing first name for fileinput.
    pw = PDFWriter('output.pdf')

    pw.setFont('Courier', 12)
    pw.setHeader('Batch text files to PDF')
    pw.setFooter('Created by xtopdf')

    for line in fileinput.input():
        pw.writeLine(line.strip('\n'))

    pw.savePage()
    pw.close()

main()

The program has no error checking and can be improved some (e.g. see the comment in the code); this is just a quick demo.

Run it like this:

python batch_text_to_pdf.py file1.txt  ...

where the  ... means (optionally)  more text file names.

The combined output should then be in file output.pdf.

xtopdf: https://bitbucket.org/vasudevram/xtopdf
ReportLab: http://www.reportlab.com/ftp
Use ReportLab v1.21, not v2.x.

Posted via mobile, sorry for narrow post width.

- Vasudev Ram
dancingbison.com

No comments: