Convert local path to UNC path in Python

I recently had the need to convert a local path to a UNC path for an application I was writing. The application monitors a local folder for files and serves them to a client over the network as requested, but for the client to be able to access the file the path had to be a UNC path.

There didn’t seem to be a built in way to do this in Python so here’s what I ended up with.

more ...

Improving PyPDF2 with PDFtk

PyPDF2 (forked from pyPdf) is wonderful. I use it a fair bit in my job, mainly for chopping up PDFs and re-assembling the pages in a different order. It does sometimes have difficulty with non-standard PDFs though that seem fine in other programs. This can be frustrating.

The one that I’ve been battling with today from some PDFs provided by a client was:

PyPDF2.utils.PdfReadError: EOF marker not found

I managed to find a workaround using PDFtk to fix the PDF in memory at the first sign of any trouble. It works well so far, so in case anyone else is having similar issues I thought I’d write it up.

more ...

New Blog!

I’ve moved this site from WordPress to Pelican. Pelican is awesome.

WordPress is written in PHP and uses MySQL to store the data needed to build each page. The good thing about WordPress is that the pages are built as they are requested. The bad thing about WordPress is that the pages are built as they are requested.

The whole database-driven dynamic page thing can slow down a site quite a bit, especially if you’re only using a basic hosting package. If like me you also install a boatload of plugins and themes to test things out the database gets full of cruft adding to more slowdowns. I experimented with various different caching plugins and they seemed to work great for a couple of weeks and then they’d stop updating the content and serve up week-old stale pages. I never found out why.

more ...

QGraphicsView with mouse wrapping

In a document management program that I’m writing I’m displaying images in a QGraphicsView. The QGraphicsView supports a drag mode that allows a user to simply click and drag within the view to pan around, which works great when a large image is displayed and the user has zoomed in quite far.

Panning the image this way does mean that the user has to click back on the image and move the mouse again when the mouse reaches the end of the screen. I needed a way to implement mouse wrapping so that when the mouse reaches the end of the QGraphicsView during panning and the user is still holding down the left mouse button the mouse will jump to the opposite edge, giving a smooth continuous pan.

Here’s what I came up with…

more ...

Using QStyledItemDelegate on a QTableView

Most databases store dates in the format YYYY-MM-DD. When using a QTableView to display a database table in PyQt it’s useful to be able to display this in a different format. Being from the UK I’d rather have the date displayed in the UK style – DD/MM/YYYY. This is where QStyledItemDelegate comes in.

I’d put off looking into this for a while because whenever I tried to look in to using QStyledItemDelegate I saw people mentioning paint methods. I know nothing about painting in PyQt and get quite scared when I see it mentioned. I really need to get over that.

Not to worry though, because it’s not needed…

more ...

Splitting old-style JPEG encoded TIFFs

The tiffsplit program from the libtiff library does a good job of splitting multi-page TIFF files into single pages. It doesn’t seem that great at handling TIFFs saved with ‘old-style’ JPEG encoding.

It’ll produce files, and the correct number of files too, but the files are actually in JPEG format. Most image viewers won’t open them still because they’re JPEG files with TIFF headers.

Assuming you have a TIFF file made up entirely of old-style JPEG encoded images you can loop through the files that tiffsplit has exported and remove the first eight bytes to give valid JPEG files.

more ...

Installing Poppler for PyQt4 on Windows

Poppler is a PDF rendering library based on the xpdf-3.0 code base. It has bindings for Qt4 which we can use through Python/PyQt. Using these libraries under most popular Linux distributions is as simple as installing the Poppler package, but under Windows I’ve found that things are a little more tricky to get working.

more ...

Native Desktop Application for Subsonic

I’ve been looking for a new personal project to work on for a while and I’ve been having trouble finding something. I wanted something that I’d use myself, otherwise my interest would quickly drop. Something that ideally I would use every day. I also wanted to write something that other people would find a use for as well. And it had to be written in Python and be something completely different to anything I’ve played with before. So I’ve decided to write a desktop client for Subsonic.

more ...