[3410] | 1 | # -*- coding: utf-8 -*- |
---|
[232] | 2 | # |
---|
| 3 | # pkpgcounter : a generic Page Description Language parser |
---|
| 4 | # |
---|
[3578] | 5 | # (c) 2003-2019 Jerome Alet <alet@librelogiciel.com> |
---|
[463] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[232] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[463] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[232] | 9 | # (at your option) any later version. |
---|
[3436] | 10 | # |
---|
[232] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
[3436] | 15 | # |
---|
[232] | 16 | # You should have received a copy of the GNU General Public License |
---|
[463] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[232] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
| 21 | |
---|
[357] | 22 | """This modules implements a page counter for OpenDocument documents.""" |
---|
| 23 | |
---|
[232] | 24 | import sys |
---|
| 25 | import zipfile |
---|
| 26 | |
---|
[235] | 27 | import pdlparser |
---|
[232] | 28 | |
---|
| 29 | class Parser(pdlparser.PDLParser) : |
---|
| 30 | """A parser for OpenOffice.org documents.""" |
---|
[526] | 31 | 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] | 32 | required = [ "xvfb-run", "xauth", "abiword", "gs" ] |
---|
[555] | 33 | format = "ISO/IEC DIS 26300" |
---|
[3436] | 34 | def isValid(self) : |
---|
[387] | 35 | """Returns True if data is OpenDocument, else False.""" |
---|
[522] | 36 | if self.firstblock[:2] == "PK" : |
---|
[232] | 37 | try : |
---|
[526] | 38 | self.archive = zipfile.ZipFile(self.filename) |
---|
[232] | 39 | self.contentxml = self.archive.read("content.xml") |
---|
| 40 | self.metaxml = self.archive.read("meta.xml") |
---|
[3436] | 41 | except : |
---|
[387] | 42 | return False |
---|
[232] | 43 | else : |
---|
[387] | 44 | return True |
---|
[3436] | 45 | else : |
---|
[387] | 46 | return False |
---|
[3436] | 47 | |
---|
[232] | 48 | def getJobSize(self) : |
---|
| 49 | """Counts pages in an OpenOffice.org document. |
---|
[3436] | 50 | |
---|
[232] | 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 |
---|