PDFWriter - a core class of the xtopdf toolkit - can now be used with a Python context manager, a.k.a. the Python with statement.
Code example below. More details here:
1 2 3 4 5 6 7 8 9 10 11 12 13
# test_pdfwriter.py from PDFWriter import PDFWriter with PDFWriter("test_pdfwriter.pdf") as pw: pw.setFont("Courier", 12) pw.setHeader("Input: test_pdfwriter.py Output: test_pdfwriter.pdf") pw.setFooter("Generated by xtopdf: http://bit.ly/xtopdf") with open("test_pdfwriter.py") as in_fil: for lin in in_fil: pw.writeLine(lin)
This recipe shows how to use PDFWriter, a class in the xtopdf tookit (a PDF creation toolkit for Python), with a context manager / with statement.
More details here:
The advantage of using the with statement with the PDFWriter constructor is that you do not have to remember to call to .close() method of PDFWriter, analogous to how you do not have to call the .close() method of a file object if you use it in a with statement.