![]() |
#1 | |||
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 776
Karma: 1538394
Join Date: Sep 2013
Device: Kobo Forma
|
REGEX to Remove Embedded Header/Footer in the Text from a PDF?
Well, that title probably makes no sense. I've got the text from a PDF file that has the header and footer information embedded within it (so the header/footer looks just like actual text). Something like:
Quote:
Code:
\s+\d+\s+TITLE\s+ \s means to match whitespace + means to match 1 or more \d means to match a digit TITLE is the title of the document that stuck in what used to be a header So, it looks like that REGEX should grab from the start of the whitespace before the page number and run through the title to the end of the whitespace where the actual text picks up again. Probably not the best bit of REGEX, but it seems to work. If the text before that header/footer combination is the end of a paragraph, that's fine. But, if the header/footer combination occurs right in the middle of a sentence, then removing it will result in the continuation "paragraph" being smashed right up against the paragraph that was before the header/footer. For instance: Quote:
Quote:
EDIT: I guess if I replace the selection with a CR LF (/r/n), that would work reasonably. It doesn't look like it would be any worse than all the other lines ending with CR LF. I'll have to check and see if Calibre's conversion routine gets rid of those. Last edited by enuddleyarbl; 03-24-2022 at 11:00 PM. |
|||
![]() |
![]() |
![]() |
#2 |
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 450
Karma: 3886916
Join Date: May 2013
Location: Ontario, Canada
Device: Kindle KB, Oasis, Pop_Os!, Kobo Forma
|
You are on the right track. As you have seen, sometimes the header or footer will be on its own, sometimes mixed in with other text. Also, maybe not in your current book but very often, you will find varied spellings in the headers/footers. III, roman 3, often appears as Ill, and so on. Titles can be just anything. It all depends on the OCR that created the text, if the text was done with OCR.
To the point where, for example, if I want an epub of something from Internet Archive, I usually find doing my own OCR is quicker and easier than trying to correct their horrible epub, where rotten headers and footers abound, along with other stuff. The result is, there is no magic expression, regex or plain, that will do the job completely. So do it in the editor, rather than at conversion. Usually with one of these I go through with several expressions, and still find the junk when scrolling slowly through. Several simple expressions are much better than trying to find one magical one to do everything. And depending on how consistent, or not, the book is, "replace all" may never be the best choice. As to the badly split paragraphs (once the headers are out of the way), some simple regex can help there. Try searching for Code:
([a-z])</p>\s+<p class="indent">([a-z]) Code:
/1 /2 Edit: You don't need CR/LF, just add a space character to your replace string so it appears between the words. (And here you would only need a newline character, \n, anyway.) Last edited by retiredbiker; 03-25-2022 at 04:02 PM. |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Guru
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 776
Karma: 1538394
Join Date: Sep 2013
Device: Kobo Forma
|
Thanks for the help. I had used Notepad++ to remove the header/footer stuff from the pure text. Since your regex was searching for HTML tags, I imported my text into a Calibre book, converted it to EPUB and edited it from within Calibre. Easy peasy.
Your replacement code should be \1 \2 instead of /1 /2. But, other than that, it seems to have worked very nicely. I'm sure there might be an instance or two where a "paragraph" ended with something like a comma instead of a lower case letter (so it wouldn't be caught). But, I'll have to read through to find them. I'd say those edits got me to a nicely readable text. Thanks, again. |
![]() |
![]() |
![]() |
#4 |
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 450
Karma: 3886916
Join Date: May 2013
Location: Ontario, Canada
Device: Kindle KB, Oasis, Pop_Os!, Kobo Forma
|
Yeah, typing off the cuff, too fast. Sorry for the /1 /2
![]() |
![]() |
![]() |
![]() |
#5 |
Addict
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 207
Karma: 304158
Join Date: Jan 2016
Location: France
Device: none
|
Before converting it to eg. EPUB, a simpler solution than regexes to get rid of headers+footers from a PDF is to… remove them from the PDF before calling Calibre :-)
Here's a script to mark those sections as "redaction annotations" and remove them. |
![]() |
![]() |
Advert | |
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Glo remove the header and footer in kepubs | tempest@de | Kobo Reader | 8 | 05-28-2015 05:40 AM |
Structure Detection - Remove Header (or Footer) Regex | DarkKipper | Conversion | 69 | 11-09-2013 12:21 PM |
software to remove pdf header, footer | cybmole | Conversion | 31 | 04-18-2011 02:37 AM |
Regex to remove header from PDF | neonbible | Calibre | 4 | 09-07-2010 10:08 AM |
PDF Conversion - Removing Header / Footer Text | heb | Sony Reader | 9 | 07-11-2010 11:02 PM |