# Author : mounicraju@gmail.com
import sys
import subprocess
from PDFWriter import PDFWriter
def error_exit(message):
sys.stderr.write(message + '\n')
sys.stderr.write("Terminating.\n")
sys.exit(1)
def main():
try:
# creating and setting up a PDFWriter instance.
pw = PDFWriter("PopenTo.pdf")
pw.setFont("Courier", 12)
pw.setHeader("Use subprocess.Popen for reading pipe and writing to PDF.")
pw.setFooter("Done using selpg, xtopdf, Python and ReportLab, on Linux.")
pipe = subprocess.Popen("nl -ba 1000-lines.txt | selpg -s3 -e5", \
shell=True, bufsize=-1, stdout=subprocess.PIPE,
stderr=sys.stderr).stdout
# Reading from the pipeline and writing the data to PDF, using the PDFWriter instance.
for idx, line in enumerate(pipe):
pw.writeLine(str(idx).zfill(8) + ": " + line)
except IOError as ioe:
error_exit("Caught IOError: {}".format(str(ioe)))
except Exception as e:
error_exit("Caught Exception: {}".format(str(e)))
finally:
pw.close()
main()
No comments:
Post a Comment