Skip to content

Repository files navigation

EPUB CSS Margin and Padding Remover

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.

Features

  • 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.

Requirements

  • Python
  • Standard Python libraries: os, zipfile, re, shutil
  • ebooklib (no longer used)
  • Calibre installed on your system

    Requirements can be installed via requirements.txt

Installation

  1. Clone the Repository
    git clone https://github.com/Taylor-eOS/epub-margins
    cd epub-margins

Or just download the .py file.

  1. Create a Virtual Environment
    python -m venv epub-margins
    cd epub-margins
    source bin/activate #On Windows: epub-margins\Scripts\activate

Usage

  1. Place EPUB Files Copy all the EPUB files you want to process into the folder input_files.
  2. Run the Script
    python reduce_all_margins.py
  3. Processed Files The processed EPUB files will be saved in the processed_epubs directory 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.

How It Works

The script performs the following steps for each EPUB file:

  1. Extracts the EPUB: Unzips the EPUB file into a temporary directory.
  2. Processes CSS Files: Searches for all .css files and replaces margin and padding properties with 0 !important.
  3. Repackages the EPUB: Zips the processed files back into an EPUB format. (Hopefully now into a proper format.)
  4. Cleanup: Removes the temporary directory used for processing.

Customization

  • 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"

Troubleshooting

  • 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.

About

Scripts for automatically editing EPUB files

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages