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

This recipe shows how to read data from a CSV file with a D program and write that data to a PDF file with a Python program - all in a single command-line invocation (after writing the individual programs, of course).

It requires the xtopdf toolkit, which you can get from:

https://bitbucket.org/vasudevram/xtopdf

Instructions for installing xtopdf:

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

xtopdf in turn requires the open source version of the ReportLab toolkit, which you can get from:

http://www.reportlab.com/ftp (http://www.reportlab.com/ftp/reportlab-1.21.1.tar.gz)

It also requires the DMD compiler to compile the D program - this was the version used:

DMD32 D Compiler v2.071.2

Python, 74 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
First, the D program, read_csv.d:

/**************************************************
File: read_csv.d
Purpose: A program to read CSV data from a file and 
write it to standard output.
Author: Vasudev Ram
Date created: 2016-10-25
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
**************************************************/

import std.algorithm;
import std.array;
import std.csv;
import std.stdio;
import std.file;
import std.typecons;

int main()
{
    try {
        stderr.writeln("Reading CSV data from file.");
        auto file = File("input.csv", "r");
        foreach (record;
            file.byLine.joiner("\n").csvReader!(Tuple!(string, string, int)))
        {
            writefln("%s works as a %s and earns $%d per year",
                     record[0], record[1], record[2]);
        }
    } catch (CSVException csve) {
        stderr.writeln("Caught CSVException: msg = ", csve.msg, 
        " at row, col = ", csve.row, ", ", csve.col);
    } catch (FileException fe) {
        stderr.writeln("Caught FileException: msg = ", fe.msg);
    } catch (Exception e) {
        stderr.writeln("Caught Exception: msg = ", e.msg);
    }
    return 0;
}

It can be compiled with the command:

dmd read_csv.d

Next the Python program, StdinToPDF.py:

# StdinToPDF.py

# Read the contents of stdin (standard input) and write it to a PDF file 
# whose name is specified as a command line argument.
# Author: Vasudev Ram - http://www.dancingbison.com
# This program is part of the xtopdf toolkit:
#     https://bitbucket.org/vasudevram/xtopdf

import sys
from PDFWriter import PDFWriter

try:
    with PDFWriter(sys.argv[1]) as pw:
        pw.setFont("Courier", 12)
        for lin in sys.stdin:
            pw.writeLine(lin)
except Exception, e:
    print "ERROR: Caught exception: " + repr(e)
    sys.exit(1)

And last, the command-line pipeline that runs the two programs above:

read_csv | python StdinToPDF.py csv_output.pdf

After running the pipeline above, the final output (the CSV data, converted to PDF) will be in the file csv_output.pdf, which you can view in any suitable PDF viewer  program, such as Foxit PDF Reader or Windows or evince on Linux.

xtopdf itself has support for reading CSV files and converting them to PDF. But this program shows another way to convert CSV to PDF - reading the CSV with D and writing the PDF with Python. Different approaches have different pros and cons, depending on the situations in which they are used, and the needs of the occasion.