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

Make whitespace checking more robust #88

Merged
merged 1 commit into from
Nov 14, 2018
Merged

Conversation

OisinMoran
Copy link
Contributor

Currently only looks for the literal " " and fails on other whitespace characters like "\xa0" (non-breaking space). This causes extract_words() to not split words on these characters, and also to pick up stray whitespace as words. Python's builtin string method isspace() is perfect for this.

I've never actually written a test before but from looking at some of yours, this might do the trick. I've attached an example document below. Currently len(words) == 32 but from looking at the document you can see it should be 25, and indeed it is when using isspace().

import pdfplumber
filename = "whitespace_test.pdf"
with pdfplumber.open(filename) as pdf:
    first_page = pdf.pages[0]
    words = first_page.extract_words()
    assert(len(words) == 25)

whitespace_test.pdf

Currently only looks for the literal " " and fails on other whitespace characters like "\xa0" (non-breaking space).
Python's builtin string method "isspace()" is perfect for this.
@jsvine jsvine merged commit 588b3f8 into jsvine:master Nov 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants