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
## 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 time
try:
basestring
except NameError:
# Python 3
basestring = str
try:
from PyPDF2.generic import ArrayObject, BooleanObject, DictionaryObject, FloatObject, NameObject, RectangleObject, TextStringObject
from PyPDF2.pdf import PdfFileReader, PdfFileWriter
@ -49,7 +57,7 @@ def annotate(fp_in, annotations):
try:
pdfpage = pdf.getPage(page)
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
size = pdfpage.mediaBox
@ -64,19 +72,19 @@ def annotate(fp_in, annotations):
else:
x = float(x)
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)
if isinstance(color, basestring):
if color[:1] != '#':
print >> sys.stderr, 'Unsupported color format: %s' % (color)
print('Unsupported color format: %s' % (color), file=sys.stderr)
color = None
else:
# Assume HTML color with format "#RRGGBB".
try:
color = int(color[1:], 16)
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
if color is not None:
@ -113,7 +121,6 @@ def annotate(fp_in, annotations):
else:
annots.append(annoRef)
fp_out = StringIO()
fp_out = BytesIO()
pdf.write(fp_out)
return fp_out.getvalue()

View file

@ -20,9 +20,10 @@
## 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/>.
##
from __future__ import print_function
import argparse
from cStringIO import StringIO
from io import BytesIO
import json
import sys
@ -42,21 +43,21 @@ def main():
args = parser.parse_args()
try:
annotations = json.loads(file(args.text, 'rb').read())
except Exception, e:
annotations = json.loads(open(args.text, 'rb').read())
except Exception as e:
parser.error('Could not open/read text annotations: %s' % (e))
if args.input == '-':
fp_in = StringIO(sys.stdin.read())
fp_in = BytesIO(sys.stdin.read())
else:
fp_in = file(args.input, 'rb')
fp_in = open(args.input, 'rb')
pdf_data = annotate(fp_in, annotations)
if args.output == '-':
sys.stdout.write(pdf_data)
else:
with file(args.output, 'wb') as fp:
with open(args.output, 'wb') as fp:
fp.write(pdf_data)
if __name__ == '__main__':