From 73688e02a9a0d87bf077b0b4b5cd942c2634573f Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Thu, 7 Apr 2022 17:20:25 +0200 Subject: [PATCH 1/2] BUG: Stream operations can be List or Dict Appeared when merging PDFs that have content-stream-inline images This patch was provided by Georg Graf : https://github.com/mstamy2/PyPDF2/issues/196#issuecomment-172829990 Thank you! Closes #196 --- PyPDF2/pdf.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/PyPDF2/pdf.py b/PyPDF2/pdf.py index fb0eccdda..f699ddc64 100644 --- a/PyPDF2/pdf.py +++ b/PyPDF2/pdf.py @@ -47,6 +47,7 @@ import struct import sys import uuid +import types from sys import version_info if version_info < ( 3, 0 ): from cStringIO import StringIO @@ -2255,10 +2256,18 @@ def _contentStreamRename(stream, rename, pdf): return stream stream = ContentStream(stream, pdf) for operands, _operator in stream.operations: - for i in range(len(operands)): - op = operands[i] - if isinstance(op, NameObject): - operands[i] = rename.get(op,op) + if type (operands) == types.ListType: + for i in range(len(operands)): + op = operands[i] + if isinstance(op, NameObject): + operands[i] = rename.get(op,op) + elif type (operands) == types.DictType: + for i in operands: + op = operands[i] + if isinstance(op, NameObject): + operands[i] = rename.get(op,op) + else: + raise KeyError ("type of operands is %s" % type (operands)) return stream _contentStreamRename = staticmethod(_contentStreamRename) From 2c3a5abb92f29ba8aed40f3f02c5cfd8115eac64 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Thu, 7 Apr 2022 17:27:48 +0200 Subject: [PATCH 2/2] Fix patch --- PyPDF2/pdf.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/PyPDF2/pdf.py b/PyPDF2/pdf.py index f699ddc64..f932d7f15 100644 --- a/PyPDF2/pdf.py +++ b/PyPDF2/pdf.py @@ -47,7 +47,6 @@ import struct import sys import uuid -import types from sys import version_info if version_info < ( 3, 0 ): from cStringIO import StringIO @@ -2256,12 +2255,12 @@ def _contentStreamRename(stream, rename, pdf): return stream stream = ContentStream(stream, pdf) for operands, _operator in stream.operations: - if type (operands) == types.ListType: + if isinstance(operands, list): for i in range(len(operands)): op = operands[i] if isinstance(op, NameObject): operands[i] = rename.get(op,op) - elif type (operands) == types.DictType: + elif isinstance(operands, dict): for i in operands: op = operands[i] if isinstance(op, NameObject):