Tag Archives: python

This Script Sets Wallpapers from Imgur As Your Desktop Background

Cinnamon 3.2 with Vertical Panels (Image: Linux Mint)Pyckground is a simple python script that can fetch a new desktop background on the Cinnamon desktop from any Imgur gallery you want. I came across it while doing a bit of background on the Bit Day wallpaper pack, and though it was nifty enough to be of use to some of you. So how does it work? Well, quite […]

This post, This Script Sets Wallpapers from Imgur As Your Desktop Background, was written by Joey-Elijah Sneddon and first appeared on OMG! Ubuntu!.

Read More

This App Sets the Bing Image of the Day as the Wallpaper on Linux

bing-to-linuxBing — no booing, folks — isn’t the web’s best search engine, but there’s no denying that it has a striking visual presence. And this is largely down to its use of stunning high-resolution imagery as its background — imagery that changes on a daily basis. If you like some of the images you come across, and want to […]

This post, This App Sets the Bing Image of the Day as the Wallpaper on Linux, was written by Joey-Elijah Sneddon and first appeared on OMG! Ubuntu!.

Read More

pip now installed with Python 2.7.9

By Vasudev Ram

In Python 2.7.9, pip (the Python package installer tool) will now be installed automatically.

Read it via a tweet and then verified it via this link:

Installing Python Modules

and also this one:

pip included with Python

Vasudev Ram – Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services from me.

Posts about Python  Posts about xtopdf

Contact Page

var addthis_config = {“data_track_clickback”:true};

Read More

Publish SQLite data to PDF using named tuples

By Vasudev Ram

Some time ago I had written this post:

Publishing SQLite data to PDF is easy with xtopdf.

It showed how to get data from an SQLite (Wikipedia) database and write it to PDF, using xtopdf, my open source PDF creation library for Python.

Today I was browsing the Python standard library docs, and so thought of modifying that program to use the namedtuple data type from the collections module of Python, which is described as implementing “High-performance container datatypes”. The collections module was introduced in Python 2.4.
Here is a modified version of that program, SQLiteToPDF.py, called SQLiteToPDFWithNamedTuples.py, that uses named tuples:

# SQLiteToPDFWithNamedTuples.py
# Author: Vasudev Ram - http://www.dancingbison.com
# SQLiteToPDFWithNamedTuples.py is a program to demonstrate how to read
# SQLite database data and convert it to PDF. It uses the Python
# data structure called namedtuple from the collections module of
# the Python standard library.

from __future__ import print_function
import sys
from collections import namedtuple
import sqlite3
from PDFWriter import PDFWriter

# Helper function to output a string to both screen and PDF.
def print_and_write(pw, strng):


# Create the stocks database.
conn = sqlite3.connect('stocks.db')
# Get a cursor to it.
curs = conn.cursor()

# Create the stocks table.
curs.execute('''DROP TABLE IF EXISTS stocks''')
curs.execute('''CREATE TABLE stocks
(date text, trans text, symbol text, qty real, price real)''')

# Insert a few rows of data into the stocks table.
curs.execute("INSERT INTO stocks VALUES ('2006-01-05', 'BUY', 'RHAT', 100, 25.1)")
curs.execute("INSERT INTO stocks VALUES ('2007-02-06', 'SELL', 'ORCL', 200, 35.2)")
curs.execute("INSERT INTO stocks VALUES ('2008-03-07', 'HOLD', 'IBM', 300, 45.3)")

# Create a namedtuple to represent stock rows.
StockRecord = namedtuple('StockRecord', 'date, trans, symbol, qty, price')

# Run the query to get the stocks data.
curs.execute("SELECT date, trans, symbol, qty, price FROM stocks")

# Create a PDFWriter and set some of its fields.
pw = PDFWriter("stocks.pdf")
pw.setFont("Courier", 12)
pw.setHeader("SQLite data to PDF with named tuples")
pw.setFooter("Generated by xtopdf - https://bitbucket.org/vasudevram/xtopdf")

# Write header info.
hdr_flds = [ str(hdr_fld).rjust(10) + " " for hdr_fld in StockRecord._fields ]
hdr_fld_str = ''.join(hdr_flds)
print_and_write(pw, '=' * len(hdr_fld_str))
print_and_write(pw, hdr_fld_str)
print_and_write(pw, '-' * len(hdr_fld_str))

# Now loop over the fetched data and write it to PDF.
# Map the StockRecord namedtuple's _make class method
# (that creates a new instance) to all the rows fetched.
for stock in map(StockRecord._make, curs.fetchall()):
row = [ str(col).rjust(10) + " " for col in (stock.date,
stock.trans, stock.symbol, stock.qty, stock.price) ]
# Above line can instead be written more simply as:
# row = [ str(col).rjust(10) + " " for col in stock ]
row_str = ''.join(row)
print_and_write(pw, row_str)

print_and_write(pw, '=' * len(hdr_fld_str))

except Exception as e:
print("ERROR: Caught exception: " + e.message)


This time I’ve imported print_function so that I can use print as a function instead of as a statement.

Here’s a screenshot of the PDF output in Foxit PDF Reader:

Vasudev Ram – Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services from me.

Posts about Python  Posts about xtopdf

Contact Page

var addthis_config = {“data_track_clickback”:true};

Read More

Excel to PDF with xlwings and xtopdf

By Vasudev Ram

Excel to PDF with xlwings and xtopdf – how many x in that? 🙂

I came across xlwings recently via the Net.

xlwings is by Zoomer Analytics, a startup based in Zürich, Switzerland, by a team with background in financial institutions.

Excerpt from the xlwings documentation:

[ xlwings is a BSD-licensed Python library that makes it easy to call Python from Excel and vice versa:

Interact with Excel from Python using a syntax that is close to VBA yet Pythonic.

Replace your VBA macros with Python code and still pass around your workbooks as easily as before.

xlwings fully supports NumPy arrays and Pandas DataFrames. It works with Microsoft Excel on Windows and Mac. ]

I checked out the xlwings quickstart.

Then did a quick test of using xlwings with xtopdf, my toolkit for PDF creation, to create a simple Excel spreadsheet, then read back its contents, and convert that to PDF.

Here is the code:

A demo program to show how to convert the text extracted from Excel
content, using xlwings, to PDF. It uses the xlwings library, to create
and read the Excel input, and the xtopdf library to write the PDF output.
Author: Vasudev Ram - http://www.dancingbison.com
Copyright 2015 Vasudev Ram

import sys
from xlwings import Workbook, Sheet, Range, Chart
from PDFWriter import PDFWriter

# Create a connection with a new workbook.
wb = Workbook()

# Create the Excel data.
# Column 1.
Range('A1').value = 'Foo 1'
Range('A2').value = 'Foo 2'
Range('A3').value = 'Foo 3'
# Column 2.
Range('B1').value = 'Bar 1'
Range('B2').value = 'Bar 2'
Range('B3').value = 'Bar 3'

pw = PDFWriter("xlwingsTo.pdf")
pw.setFont("Courier", 10)
pw.setHeader("Testing Excel conversion to PDF with xlwings and xtopdf")
pw.setFooter("xlwings: http://xlwings.org --- xtopdf: http://slid.es/vasudevram/xtopdf")

for row in Range('A1..B3').value:
s = ''
for col in row:
s += col + ' | '


I ran it with this command:

py xlwingsToPDF.py

and here is a screenshot of the output PDF file:

Note: The xlwings library can be installed with:

pip install xlwings

But a prerequisite for it, pywin32, did not install automatically. pywin32 is a very useful and powerful Windows API wrapper library for Python, by Mark Hammond. I’ve used it a few times earlier, in earlier Python versions than Python 2.7.8, which I currently am using. I usually installed it directly in those earlier versions. This time, though it was a dependency for xlwings, it did not get installed automatically, and the above Python program gave a runtime error. I had to manually install pywin32 before the program could work.

– Enjoy.

Vasudev Ram – Dancing Bison Enterprises

Signup to hear about new products or services from me.

Contact Page

var addthis_config = {“data_track_clickback”:true};

Read More

Effective Python book now available in digital edition

By Vasudev Ram

Saw this via an email from the book’s author, Brett Slatkin:

The book Effective Python is now available in digital editions. The author, Brett Slatkin, is a Senior Staff Software Engineer at Google. From the book’s site:

[ He is the engineering lead and co-founder of Google Consumer Surveys. He formerly worked on Google App Engine’s Python infrastructure. He is the co-creator of the PubSubHubbub protocol. Nine years ago he cut his teeth using Python to manage Google’s enormous fleet of servers. ]

From the description of the book on the site:

[ The Python programming language has unique strengths and charms that can be hard to grasp. Many programmers familiar with other languages often approach Python from a limited mindset instead of embracing its full expressivity. Some programmers go too far in the other direction, overusing Python features that can cause big problems later.

Effective Python provides insight into the Pythonic way of writing programs: the best way to use Python. It builds on a fundamental understanding of the language that I assume you already have. Novice programmers will learn the best practices of Python’s capabilities. Experienced programmers will learn how to embrace the strangeness of a new tool with confidence. ]

The book is a part of the Effective Software Development series of books, with Scott Meyers as the Consulting Editor (for the series). Meyers wrote both the books Effective C++ and More Effective C++, some years ago. I had read the former book (Effective C++) pretty much fully, soon after it came out, and it was very good. IIRC, it was the success of Effective C++ [1] that lead to the decision to create the Effective Software Development series.

[1] I was told by an acquisitions editor at McGraw-Hill that Effective C++ sold in the neighborhood of 50,000 copies.

Vasudev Ram – Dancing Bison Enterprises

Signup to hear about new products or services from me.

Contact Page

var addthis_config = {“data_track_clickback”:true};

Read More

Introducing ePad – A text editor written in Elementary

I have been working on a number of small tools for the up coming Bodhi Linux 3.0.0 release (find the latest release candidate here) and one than I am working hard to get up to snuff is our text editor ePad. Previous versions of Bodhi Linux has always included the GTK text editor Leafpad by default so my initial release of ePad has the goal of simply covering the same features as Leafpad at the bare minimum.

As of today my ePad source is nearing a 1.0.0 release and the goal of this post is to let folks outside of Bodhi know it exists and that it is ready for them to give it a try. ePad in its current form supports most all of the features you would expect from a text editor: Cut, Copy, Paste, Undo, Redo, Find, Replace, and displaying Line Numbers. On top of these it also supports opening multiple files in the same application window.

You can find the ePad source on GitHub here and you can report issues you encounter on the GitHub issue tracker here. Please note that ePad requires:

Finally a few screenshots of ePad in a couple different elementary themes:

~Jeff Hoogland

Read More

Recursively dumping the structure of an HTML5 document

By Vasudev Ram

A while ago I had written this post,

The html5lib Python library (and Animatron 🙂

which shows basic usage of a Python library called html5lib, that lets you parse HTML5 documents and then walk through their structure.

That post uses this HTML5 document as input for the program shown in it:

Yesteday I modified the program (test_html5lib.py) shown in that earlier post, to make it recursive, thereby simplifying it. Here is the code for the resulting program, html5_dump.py.

# Demo program to show how to dump the structure of 
# an HTML5 document to text, using html5lib.
# Author: Vasudev Ram.
# Copyright 2015 Vasudev Ram - http://www.dancingbison.com

import html5lib

# Define a function to dump HTML5 element info recursively,
# given a top-level element.
def print_element(elem, indent, level):
for sub_elem in elem:
print "{}{}".format(indent * level, sub_elem)
# Recursive call to print_element().
print_element(sub_elem, indent, level + 1)

f = open("html5doc.html")
# Parse the HTML document.
tree = html5lib.parse(f)
indent = '----'
level = 0
print_element(tree, indent, level)

I ran the program with:
$ py html5_dump.py

where the py in the command refers to py, the Python Launcher for Windows

Here is the program output, which you can see is basically the same as the previous version, but, done using recursion.


The recursion helps in two ways: 1) recursively printing sub-elements, and 2) not having to keep track of the indentation level needed – the Python interpreter’s handling of nested calls and backing out of them, takes care of that for us. See the line:

print_element(sub_elem, indent, level + 1)

However, if using deep recursion, we have to remember about python recursion depth issues.


Vasudev Ram – online Python trainer and freelance programmer

Seeking alpha …

Signup to hear about my new software products.

Contact Page

Sub-feeds for my posts about Python and posts about xtopdf.

var addthis_config = {“data_track_clickback”:true};

Read More

HTML text to PDF with Beautiful Soup and xtopdf

By Vasudev Ram

Recently, I thought of getting the text from HTML documents and putting that text to PDF. So I did it 🙂

Here’s how:

A demo program to show how to convert the text extracted from HTML
content, to PDF. It uses the Beautiful Soup library, v4, to
parse the HTML, and the xtopdf library to generate the PDF output.
Beautiful Soup is at: http://www.crummy.com/software/BeautifulSoup/
xtopdf is at: https://bitbucket.org/vasudevram/xtopdf
Guide to using and installing xtopdf: http://jugad2.blogspot.in/2012/07/guide-to-installing-and-using-xtopdf.html
Author: Vasudev Ram - http://www.dancingbison.com
Copyright 2015 Vasudev Ram

import sys
from bs4 import BeautifulSoup
from PDFWriter import PDFWriter

def usage():
sys.stderr.write("Usage: python " + sys.argv[0] + " html_file pdf_filen")
sys.stderr.write("which will extract only the text from html_file andn")
sys.stderr.write("write it to pdf_filen")

def main():

# Create some HTML for testing conversion of its text to PDF.
html_doc = """

<br /> Test file for HTMLTextToPDF<br />

This is text within the body element but outside any paragraph.

This is a paragraph of text. Hey there, how do you do?
The quick red fox jumped over the slow blue cow.

This is another paragraph of text.
Don't mind what it contains.
What is mind? Not matter.
What is matter? Never mind.

This is also text within the body element but not within any paragraph.


pw = PDFWriter("HTMLTextTo.pdf")
pw.setFont("Courier", 10)
pw.setHeader("Conversion of HTML text to PDF")
pw.setFooter("Generated by xtopdf: http://slid.es/vasudevram/xtopdf")

# Use method chaining this time.
for line in BeautifulSoup(html_doc).get_text().split("n"):

if __name__ == '__main__':

The program uses the Beautiful Soup library for parsing and extracting information from HTML, and xtopdf, my Python library for PDF generation.
Run it with:

python HTMLTextToPDF.py

and the output will be in the file HTMLTextTo.pdf.
Screenshot below:

Vasudev Ram – Python training and programming – Dancing Bison Enterprises

Read more of my posts about Python or read posts about xtopdf (latter is subset of former)

Signup to hear about my new software products or services.

Contact Page

var addthis_config = {“data_track_clickback”:true};

Read More