2011-12-27 92 views

回答

1
+0

有沒有一些文件或一些基本的用法描述? – xralf 2011-12-27 15:43:06

+0

pychm是隻讀的。 – 2012-09-12 09:54:34

+0

這是我的[從chm文件中提取所有html頁面的配方](http://code.activestate.com/recipes/502221-extracting-data-from-chm-microsoft-compiled-html/) – jcubic 2017-01-10 12:44:41

0

我已經與PyCHM努力創建簡單thumbnailer的從.chm文件提取封面圖片。這裏是所有那些誰在未來找到這個問題的代碼:

#!/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])