Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated extractText() #397

Closed
wants to merge 6 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 35 additions & 7 deletions PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2679,7 +2679,7 @@ def compressContentStreams(self):
content = ContentStream(content, self.pdf)
self[NameObject("/Contents")] = content.flateEncode()

def extractText(self, Tj_sep="", TJ_sep=" "):
def extractText(self, Tj_sep="", TJ_sep=" ", skip_intertwined_text=True):
"""
Locate all text drawing commands, in the order they are provided in the
content stream, and extract the text. This works well for some PDF
Expand All @@ -2697,13 +2697,18 @@ def extractText(self, Tj_sep="", TJ_sep=" "):
# Note: we check all strings are TextStringObjects. ByteStringObjects
# are strings where the byte->string encoding was unknown, so adding
# them to the text here would be gibberish.
#
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
indent = 0
previous_width = 0
skip_next = False
for operands, operator in content.operations:
if not operands: # Empty operands list contributes no text
operands = [""]
if operator == b_("Tj"):
_text = operands[0]
if isinstance(_text, TextStringObject):
text += Tj_sep
text += _text
text += "\n"
elif operator == b_("T*"):
text += "\n"
elif operator == b_("'"):
Expand All @@ -2717,11 +2722,34 @@ def extractText(self, Tj_sep="", TJ_sep=" "):
text += "\n"
text += _text
elif operator == b_("TJ"):
for i in operands[0]:
if isinstance(i, TextStringObject):
text += TJ_sep
text += i
text += "\n"
if skip_intertwined_text and skip_next:
skip_next = False
else:
for i in operands[0]:
if isinstance(i, TextStringObject):
text += TJ_sep
previous_width += len(i)
elif isinstance(i, FloatObject) or isinstance(i, NumberObject):
if text and (not text[-1] in " \n"):
text += " " * int(i / -600)
previous_width += int(i / -600)
elif operator == b_("Td"):
indent = indent + operands[0]
if operands[1] == 0:
if int(operands[0] / 20) >= previous_width:
text += " " * (int(operands[0] / 20) - previous_width)
else:
skip_next = True
# If skip_intertwined_text is false, this will result in no space between the two 'lines'
else:
previous_width = 0
text += "\n" * max(0, int(operands[1] / -50)) + " " * max(0, int(indent / 20))
elif operator == b_("Tm"):
indent = operands[4]
text += " " * max(0, int(indent / 20))
elif operator == b_("TD") or operator == b_("Tm"):
if text and (not text[-1] in " \n"):
text += " "
return text

mediaBox = createRectangleAccessor(PG.MEDIABOX, ())
Expand Down