Skip to content

Add functionality to replace text - #482

Closed
ArifRasim wants to merge 1 commit into
camelot-dev:masterfrom
ArifRasim:ar/text-replace
Closed

ArifRasim wants to merge 1 commit into
camelot-dev:masterfrom
ArifRasim:ar/text-replace

Conversation

@ArifRasim

@ArifRasim ArifRasim commented Jan 13, 2024 •

Copy link
Copy Markdown

Added feature replace text. SImilar to strip_text now texts can be replaced as key value pairs when passed to the read_pdf function.
Issue #481

@MartinThoma

Copy link
Copy Markdown
Collaborator

Hey!

As camelot is dead, we try to build a maintained fork at pypdf_table_extraction.

Do you want to open the PR against that branch so that we can merge your improvement?

bosd added a commit to bosd/camelot that referenced this pull request May 20, 2026
bosd added a commit to bosd/camelot that referenced this pull request May 20, 2026
…elot-dev#482)

text_strip already lets the caller remove characters / substrings.
The 2024 feature request camelot-dev#481 by @ArifRasim asked for the symmetric
operation: replace a substring with a different one. Motivating
example was '\n' → ' ' to fix words that PDF text extraction had
split across line breaks.

API: read_pdf(..., replace_text={pattern: replacement, ...}). Keys
are literal substrings (regex metacharacters escaped), and when
several keys could match at the same position the longest one
wins (the obvious user expectation; the implementation pre-sorts
by length descending).

Implementation:

* New utils.text_replace(text, replacements) helper, dict or
  list-of-pairs accepted, empty keys skipped, Unicode-safe.
* New replace_text= constructor argument on BaseParser /
  TextBaseParser / Stream / Network / Lattice / Hybrid; stored as
  self.replace_text on the parser.
* Applied at the single central cell-text-emission site in
  BaseParser._generate_table_pos_errors (after the existing
  strip/split/flag-size pipeline). One spot; no plumbing down
  into flag_font_size etc.
* 'replace_text' added to common_kwargs so validate_input /
  remove_extra accept it for all flavors.

Closes camelot-dev#481. Closes camelot-dev#482.
bosd added a commit to bosd/camelot that referenced this pull request May 20, 2026
bosd added a commit to bosd/camelot that referenced this pull request May 20, 2026
…elot-dev#482)

text_strip already lets the caller remove characters / substrings.
The 2024 feature request camelot-dev#481 by @ArifRasim asked for the symmetric
operation: replace a substring with a different one. Motivating
example was '\n' → ' ' to fix words that PDF text extraction had
split across line breaks.

API: read_pdf(..., replace_text={pattern: replacement, ...}). Keys
are literal substrings (regex metacharacters escaped), and when
several keys could match at the same position the longest one
wins (the obvious user expectation; the implementation pre-sorts
by length descending).

Implementation:

* New utils.text_replace(text, replacements) helper, dict or
  list-of-pairs accepted, empty keys skipped, Unicode-safe.
* New replace_text= constructor argument on BaseParser /
  TextBaseParser / Stream / Network / Lattice / Hybrid; stored as
  self.replace_text on the parser.
* Applied at the single central cell-text-emission site in
  BaseParser._generate_table_pos_errors (after the existing
  strip/split/flag-size pipeline). One spot; no plumbing down
  into flag_font_size etc.
* 'replace_text' added to common_kwargs so validate_input /
  remove_extra accept it for all flavors.

Closes camelot-dev#481. Closes camelot-dev#482.
bosd added a commit to bosd/camelot that referenced this pull request May 20, 2026
@bosd bosd closed this in #755 May 20, 2026
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