xralf
xralf

Reputation: 3762

Processing chm files

Is there a Python library I can use for processing files with chm extension that has similar features as HTML parser or BeautifulSoup?

Upvotes: 0

Views: 4770

Answers (2)

Nykakin
Nykakin

Reputation: 8747

I've struggled with PyCHM to create simple thumbnailer extracting cover images from .chm files. Here is the code for all those who find this question in the future:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import chm.chm as chm
from bs4 import BeautifulSoup
from PIL import Image
import urlparse
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO

class CHMFile:
    def __init__(self, file_name):
        self.chmfile = chm.CHMFile()
        self.chmfile.LoadCHM(file_name)

    def create_thumb(self, out_file):       
        image = None
        area = 0 # cover will propably be the biggest image from home page

        iui = self.chmfile.ResolveObject(self.chmfile.home) 
        home = self.chmfile.RetrieveObject(iui[1])[1] # get home page (as html)
        tree = BeautifulSoup(home)
        for img in tree.find_all('img'):
            src_attr =  urlparse.urljoin(self.chmfile.home, img.get('src'))
            chm_image = self.chmfile.ResolveObject(src_attr)
            png_data = self.chmfile.RetrieveObject(chm_image[1])[1] # get image (as raw data)

            png_img = Image.open(StringIO(png_data))
            new_width, new_height = png_img.size
            new_area = new_width * new_height 
            if(new_area > area and new_width > 50 and new_height > 50): # to ensure image is at least 50x50
                area = new_area
                image = png_img
        if image:
            image.save(out_file, format="PNG")


if __name__ == '__main__':
    import sys
    if len(sys.argv) != 3:
        print 'Create thumbnail image from an chm file'
        print 'Usage: %s INFILE OUTFILE' % sys.argv[0]
    else:
        chm = CHMFile(sys.argv[1])
        chm.create_thumb(sys.argv[2])

Upvotes: 0

Frank Fang
Frank Fang

Reputation: 1119

PyCHM:

http://gnochm.sourceforge.net/pychm.html

Upvotes: 1

Related Questions