[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> |
---|
[232] | 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 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 |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | # |
---|
| 21 | # $Id$ |
---|
| 22 | # |
---|
| 23 | |
---|
[357] | 24 | """This modules implements a page counter for OpenDocument documents.""" |
---|
| 25 | |
---|
[232] | 26 | import sys |
---|
| 27 | import zipfile |
---|
| 28 | |
---|
[235] | 29 | import pdlparser |
---|
[232] | 30 | |
---|
| 31 | class Parser(pdlparser.PDLParser) : |
---|
| 32 | """A parser for OpenOffice.org documents.""" |
---|
| 33 | def isValid(self) : |
---|
[387] | 34 | """Returns True if data is OpenDocument, else False.""" |
---|
[232] | 35 | if self.firstblock[:2] == "PK" : |
---|
| 36 | try : |
---|
| 37 | self.archive = zipfile.ZipFile(self.infile) |
---|
| 38 | self.contentxml = self.archive.read("content.xml") |
---|
| 39 | self.metaxml = self.archive.read("meta.xml") |
---|
| 40 | except : |
---|
[387] | 41 | return False |
---|
[232] | 42 | else : |
---|
[387] | 43 | self.logdebug("DEBUG: Input file is in the OpenDocument (ISO/IEC DIS 26300) format.") |
---|
| 44 | return True |
---|
[232] | 45 | else : |
---|
[387] | 46 | return False |
---|
[232] | 47 | |
---|
| 48 | def getJobSize(self) : |
---|
| 49 | """Counts pages in an OpenOffice.org document. |
---|
| 50 | |
---|
| 51 | Algorithm by Jerome Alet. |
---|
| 52 | """ |
---|
| 53 | pagecount = 0 |
---|
| 54 | try : |
---|
| 55 | # First try with Text documents |
---|
| 56 | index = self.metaxml.index("meta:page-count=") |
---|
| 57 | pagecount = int(self.metaxml[index:].split('"')[1]) |
---|
| 58 | except : |
---|
| 59 | # Now try with Impress documents |
---|
| 60 | pagecount = self.contentxml.count("<draw:page ") |
---|
| 61 | if not pagecount : |
---|
| 62 | # Probably a Spreadsheet document |
---|
| 63 | raise pdlparser.PDLParserError, "OpenOffice.org's spreadsheet documents are not yet supported." |
---|
| 64 | return pagecount |
---|
| 65 | |
---|
| 66 | if __name__ == "__main__" : |
---|
[415] | 67 | pdlparser.test(Parser) |
---|