Update text annotation tool to support Python3.

This commit is contained in:
Joachim Bauch 2021-12-03 16:00:24 +01:00
parent 11c946de09
commit ee9ca30d74
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
2 changed files with 21 additions and 13 deletions

View file

@ -18,10 +18,18 @@
## You should have received a copy of the GNU Affero General Public License ## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <https://www.gnu.org/licenses/>. ## along with this program. If not, see <https://www.gnu.org/licenses/>.
## ##
from cStringIO import StringIO from __future__ import print_function
from io import BytesIO
import sys import sys
import time import time
try:
basestring
except NameError:
# Python 3
basestring = str
try: try:
from PyPDF2.generic import ArrayObject, BooleanObject, DictionaryObject, FloatObject, NameObject, RectangleObject, TextStringObject from PyPDF2.generic import ArrayObject, BooleanObject, DictionaryObject, FloatObject, NameObject, RectangleObject, TextStringObject
from PyPDF2.pdf import PdfFileReader, PdfFileWriter from PyPDF2.pdf import PdfFileReader, PdfFileWriter
@ -49,7 +57,7 @@ def annotate(fp_in, annotations):
try: try:
pdfpage = pdf.getPage(page) pdfpage = pdf.getPage(page)
except IndexError: except IndexError:
print >> sys.stderr, 'Page %d not found in pdf, not adding annotations %r' % (page, annotation) print('Page %d not found in pdf, not adding annotation %r' % (page, annotation), file=sys.stderr)
continue continue
size = pdfpage.mediaBox size = pdfpage.mediaBox
@ -64,19 +72,19 @@ def annotate(fp_in, annotations):
else: else:
x = float(x) x = float(x)
y = float(y) y = float(y)
print >> sys.stderr, 'Page rotated by %d degrees not implemented yet' % (angle) print('Page rotated by %d degrees not implemented yet' % (angle), file=sys.stderr)
color = annotation.get('color', None) color = annotation.get('color', None)
if isinstance(color, basestring): if isinstance(color, basestring):
if color[:1] != '#': if color[:1] != '#':
print >> sys.stderr, 'Unsupported color format: %s' % (color) print('Unsupported color format: %s' % (color), file=sys.stderr)
color = None color = None
else: else:
# Assume HTML color with format "#RRGGBB". # Assume HTML color with format "#RRGGBB".
try: try:
color = int(color[1:], 16) color = int(color[1:], 16)
except ValueError as e: except ValueError as e:
print >> sys.stderr, 'Unsupported color format: %s (%s)' % (color, e) print('Unsupported color format: %s (%s)' % (color, e), file=sys.stderr)
color = None color = None
if color is not None: if color is not None:
@ -113,7 +121,6 @@ def annotate(fp_in, annotations):
else: else:
annots.append(annoRef) annots.append(annoRef)
fp_out = StringIO() fp_out = BytesIO()
pdf.write(fp_out) pdf.write(fp_out)
return fp_out.getvalue() return fp_out.getvalue()

View file

@ -20,9 +20,10 @@
## You should have received a copy of the GNU Affero General Public License ## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <https://www.gnu.org/licenses/>. ## along with this program. If not, see <https://www.gnu.org/licenses/>.
## ##
from __future__ import print_function
import argparse import argparse
from cStringIO import StringIO from io import BytesIO
import json import json
import sys import sys
@ -42,21 +43,21 @@ def main():
args = parser.parse_args() args = parser.parse_args()
try: try:
annotations = json.loads(file(args.text, 'rb').read()) annotations = json.loads(open(args.text, 'rb').read())
except Exception, e: except Exception as e:
parser.error('Could not open/read text annotations: %s' % (e)) parser.error('Could not open/read text annotations: %s' % (e))
if args.input == '-': if args.input == '-':
fp_in = StringIO(sys.stdin.read()) fp_in = BytesIO(sys.stdin.read())
else: else:
fp_in = file(args.input, 'rb') fp_in = open(args.input, 'rb')
pdf_data = annotate(fp_in, annotations) pdf_data = annotate(fp_in, annotations)
if args.output == '-': if args.output == '-':
sys.stdout.write(pdf_data) sys.stdout.write(pdf_data)
else: else:
with file(args.output, 'wb') as fp: with open(args.output, 'wb') as fp:
fp.write(pdf_data) fp.write(pdf_data)
if __name__ == '__main__': if __name__ == '__main__':