root / pkpgcounter / trunk / pkpgpdls / ooo.py @ 555

Revision 555, 2.5 kB (checked in by jerome, 16 years ago)

Each parser now has a 'format' attribute containing its short name.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[232]1#! /usr/bin/env python
2# -*- coding: ISO-8859-15 -*-
3#
4# pkpgcounter : a generic Page Description Language parser
5#
[443]6# (c) 2003, 2004, 2005, 2006, 2007 Jerome Alet <alet@librelogiciel.com>
[463]7# This program is free software: you can redistribute it and/or modify
[232]8# it under the terms of the GNU General Public License as published by
[463]9# the Free Software Foundation, either version 3 of the License, or
[232]10# (at your option) any later version.
[463]11#
[232]12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
[463]18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[232]19#
20# $Id$
21#
22
[357]23"""This modules implements a page counter for OpenDocument documents."""
24
[232]25import sys
26import zipfile
27
[235]28import pdlparser
[232]29
30class Parser(pdlparser.PDLParser) :
31    """A parser for OpenOffice.org documents."""
[526]32    totiffcommands = [ 'xvfb-run -a abiword --import-extension=.odt --print="| gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r\"%(dpi)i\" -sOutputFile=\"%(outfname)s\" -" "%(infname)s"' ]
[527]33    required = [ "xvfb-run", "xauth", "abiword", "gs" ]
[555]34    format = "ISO/IEC DIS 26300"
[232]35    def isValid(self) :       
[387]36        """Returns True if data is OpenDocument, else False."""
[522]37        if self.firstblock[:2] == "PK" :
[232]38            try :
[526]39                self.archive = zipfile.ZipFile(self.filename)
[232]40                self.contentxml = self.archive.read("content.xml")
41                self.metaxml = self.archive.read("meta.xml")
42            except :   
[387]43                return False
[232]44            else :
[387]45                return True
[232]46        else :   
[387]47            return False
[232]48           
49    def getJobSize(self) :
50        """Counts pages in an OpenOffice.org document.
51       
52           Algorithm by Jerome Alet.
53        """
54        pagecount = 0
55        try :
56            # First try with Text documents
57            index = self.metaxml.index("meta:page-count=")
58            pagecount = int(self.metaxml[index:].split('"')[1])
59        except :
60            # Now try with Impress documents
61            pagecount = self.contentxml.count("<draw:page ")
62            if not pagecount :
63                # Probably a Spreadsheet document
64                raise pdlparser.PDLParserError, "OpenOffice.org's spreadsheet documents are not yet supported."
65        return pagecount
Note: See TracBrowser for help on using the browser.