I have some ebooks which are converted from other formats to ePub. Some are ill-formatted: there are hard line breaks in words and orphaned page numbers between paragraphs.

Screenshot from Calibre

How can I repair these ebooks? Do I have to open and edit the source files, or is there a better way?

I use Calibre on Windows to organize the ebooks, if that matters, but I am not bound to that program, and I can use Linux too.

  • thank you guysfor all your tips from 2014 ...they have saved me now in 2021! Commented Feb 6, 2021 at 12:50

6 Answers 6


Calibre has a feature that allows you to unpack an ePub file into the component parts (usually chapters), which you can then edit. When you have finished your edits, Calibre will repackage them back into an ePub file.

From the main Calibre view, right click on the book listing. You should get a popup menu with an option to "Edit book". Selecting this will give you a book editing window where you can edit the individual parts of the book.


Somehow I missed that … thanks to Donald.McLean’s answer, I found it.

In the ePub editor, I hit Ctrl+F, and a search & replace tool showed up at the bottom:

enter image description here

I have used the regex mode and the following patterns:

  • -</p>\n<p[^>]*> replaced with nothing to remove paragraph breaks within words. I hit Replace all here.

  • \s</p>\n<p[^>]*> replaced with a single space to remove paragraph breaks within sentences. This had to be done manually, unfortunately, because there were some spaces at the end of lines which should stay separated.

  • \n<p[^>]*>\d+</p> replaced with nothing to remove orphaned page numbers. Replace all again.

I have also installed, but not yet tested, the plugin Modify ePub by Grant Drake. It offers some automated tasks:

enter image description here


In Calibre you can play around Heuristic Processing option while converting your files. Calibre book converting options


I have not found an elegant way to do this yet. However, the inelegant way works: 1. Highlight on the epub to clean. 2. Click on convert. 3. Set the conversion settings to clean all the things you want (especially in the heuristics section to scan and fix things) and make sure that the output file is epub format.

It will save the original messy file as "original_epub" and create a second epub file.

  • 2
    Is it Calibre that you're using for this suggestion?
    – Tom
    Commented Aug 6, 2014 at 23:51
  • These instructions do apply to Calibre. The answer above this one (at present) by @valery-noname has a more complete description.
    – John T
    Commented Jan 2, 2023 at 7:43

Since Epubs are just zip archives, you can unzip them and operate on the plain text files underneath. From there, you can use Python or just the find and replace function in a text editor to remove the offending characters.

One thing to note is that when you rezip it, it is critical that the mimetype file is put in the archive first. Additionally, many readers require the zip file to have no compression, and all require the mimetype to be uncompressed. Check out this answer to make sure you put it back together correctly.


I used "wondershare PDF converter" to convert my pdf to epub, then I opened the epub with Calibre and removed the line breaks with these regex :

<span style="">([^.><]*)</span>\s*</p>\s*<p>

replaced by

<span style="">\1</span>



replaced by


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.