This is a Python script to process EPUB files by removing or resetting all CSS margin and padding properties within the EPUB's CSS files. This tool is useful for users who want consistent formatting across their EPUB files, in order to not waste screen real estate on an eReader.
Warning: This script was generated by a beginner with a chatbot, and proper functionality is not guranteed. Back up your original files in case problems emerge.
- Batch Processing: Automatically processes all EPUB files in a folder.
- Customizable: You can modify the script to change the margin and padding values.
- Non-destructive: Creates a new processed EPUB without altering the original file.
- Python
- Standard Python libraries:
os,zipfile,re,shutil ebooklib(no longer used)Calibreinstalled on your systemRequirements can be installed via requirements.txt
- Clone the Repository
git clone https://github.com/Taylor-eOS/epub-margins cd epub-margins
Or just download the .py file.
- Create a Virtual Environment
python -m venv epub-margins cd epub-margins source bin/activate #On Windows: epub-margins\Scripts\activate
- Place EPUB Files
Copy all the EPUB files you want to process into the folder
input_files. - Run the Script
python reduce_all_margins.py
- Processed Files
The processed EPUB files will be saved in the
processed_epubsdirectory and tell you whether the process succeeded. Error messages about inability to add custom fields do not prevent the process, but just tell you those fields don't get added.
The script performs the following steps for each EPUB file:
- Extracts the EPUB: Unzips the EPUB file into a temporary directory.
- Processes CSS Files: Searches for all
.cssfiles and replaces margin and padding properties with0 !important. - Repackages the EPUB: Zips the processed files back into an EPUB format. (Hopefully now into a proper format.)
- Cleanup: Removes the temporary directory used for processing.
-
Changing Margin and Padding Values If you want to set different values for margins and padding, modify the following variables at the top of the script:
NEW_MARGIN = "0 !important" NEW_PADDING = "0 !important"
-
No EPUB Files Found If you receive a "No EPUB files found" message, ensure that your EPUB files are in the same directory as the script.
-
Encoding Issues If you encounter encoding errors, you may need to adjust the file reading/writing encoding in the script.