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

ENH: Improve space setting for text extraction #922

Merged
merged 2 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions PyPDF2/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,34 +1080,64 @@ def extract_text(self, Tj_sep: str = "", TJ_sep: str = "") -> str:
# 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.

space_scale = 1.0

for operands, operator in content.operations:
if operator == b_("Tj"):
if operator == b_("Tf"): # text font
pass
elif operator == b_("Tfs"): # text font size
pass
elif operator == b_("Tc"): # character spacing
# See '5.2.1 Character Spacing'
pass
elif operator == b_("Tw"): # word spacing
# See '5.2.2 Word Spacing'
space_scale = 1.0 + float(operands[0])
elif operator == b_("Th"): # horizontal scaling
# See '5.2.3 Horizontal Scaling'
pass
elif operator == b_("Tl"): # leading
# See '5.2.4 Leading'
pass
elif operator == b_("Tmode"): # text rendering mode
# See '5.2.5 Text Rendering Mode'
pass
elif operator == b_("Trise"): # text rise
# See '5.2.6 Text Rise'
pass
elif operator == b_("Tj"):
# See 'TABLE 5.6 Text-showing operators'
_text = operands[0]
if isinstance(_text, TextStringObject):
text += Tj_sep
text += _text
text += "\n"
elif operator == b_("T*"):
# See 'TABLE 5.5 Text-positioning operators'
text += "\n"
elif operator == b_("'"):
# See 'TABLE 5.6 Text-showing operators'
text += "\n"
_text = operands[0]
if isinstance(_text, TextStringObject):
text += operands[0]
elif operator == b_('"'):
# See 'TABLE 5.6 Text-showing operators'
_text = operands[2]
if isinstance(_text, TextStringObject):
text += "\n"
text += _text
elif operator == b_("TJ"):
# See 'TABLE 5.6 Text-showing operators'
for i in operands[0]:
if isinstance(i, TextStringObject):
text += TJ_sep
text += i
elif isinstance(i, NumberObject):
elif isinstance(i, (NumberObject, FloatObject)):
# a positive value decreases and the negative value increases
# space
if int(i) < 0:
if int(i) < -space_scale * 250:
if len(text) == 0 or text[-1] != " ":
text += " "
else:
Expand Down
18 changes: 9 additions & 9 deletions resources/crazyones.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
The Crazy Ones
October 14, 1998
Heres to the crazy ones. The mis˝ts. The reb els. The troublemakers.
The round p egs in the square holes.
Heres to the crazy ones. The mis˝ts. The rebels. The troublemakers.
The round pegs in the square holes.
The ones who see things di˙erently. Theyre not fond of rules. And
they have no resp ect for the status quo. You can quote them,
they have no respect for the status quo. You can quote them,
disagree with them, glorify or vilify them.
Ab out the only thing you cant do is ignore them. Because they change
About the only thing you cant do is ignore them. Because they change
things. They invent. They imagine. They heal. They explore. They
create. They inspire. They push the human race forward.
Mayb e they have to b e crazy.
Maybe they have to be crazy.
How else can you stare at an empty canvas and see a work of art? Or
sit in silence and hear a song thats never b een written? Or gaze at
a red planet and see a lab oratory on wheels?
We make to ols for these kinds of p eople.
sit in silence and hear a song thats never been written? Or gaze at
a red planet and see a laboratory on wheels?
We make tools for these kinds of people.
While some see them as the crazy ones, we see genius. Because the
p eople who are crazy enough to think they can change the world,
people who are crazy enough to think they can change the world,
are the ones who do.