### EPUB Conversion CLI Complete Examples Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_convert.md A collection of complete command-line examples for the EPUB converter, covering default behavior, custom directories for both Markdown and plain text, and relative path usage. ```bash # Default: Convert to Markdown from input/ to output/ python epub_convert.py # Convert all EPUBs in 'books/' to Markdown in 'markdown_output/' python epub_convert.py --input books --output markdown_output # Convert all EPUBs in 'books/' to plain text in 'text_output/' python epub_convert.py --txt --input books --output text_output # Explicit Markdown with custom directories python epub_convert.py --md --input /home/user/epubs --output /home/user/converted # Use relative paths with custom directories python epub_convert.py --txt --input ../epubs --output ../text_files ``` -------------------------------- ### Bash Example: CLI Usage with Defaults Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Demonstrates running the `epub_to_txt.py` script using default input and output directories ('input/' and 'output/'). ```bash python epub_to_txt.py ``` -------------------------------- ### Use Case: Create Markdown Documentation Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Command-line example to convert EPUB files into Markdown format, useful for generating documentation. ```bash python epub_convert.py --input source_epubs --output docs ``` -------------------------------- ### Progress Bar Example Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/configuration.md Demonstrates the visual progress bar displayed during batch conversion operations using tqdm. ```text Converting EPUBs: [████████░░░░░░░░░░] 50% ``` -------------------------------- ### Bash Example: CLI Usage with Custom Directories Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Illustrates how to specify custom input and output directories when running the `epub_to_txt.py` script from the command line. ```bash python epub_to_txt.py --input books --output text_output ``` -------------------------------- ### Install Dependencies Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Installs the necessary Python packages for epub2md. Run this command before using the tool. ```bash pip install ebooklib beautifulsoup4 tqdm ``` -------------------------------- ### Install Python Libraries Source: https://github.com/mulualem-tekle/epub2md/blob/main/readme.md Installs the necessary Python libraries for the EPUB converter. Ensure you have Python 3.6 or higher. ```bash pip install EbookLib beautifulsoup4 tqdm ``` -------------------------------- ### Bash Example: CLI Usage with Specific Paths Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Shows how to use the command-line interface to convert EPUB files from a specific source directory to a specific output directory. ```bash python epub_to_txt.py --input /path/to/epubs --output /path/to/output ``` -------------------------------- ### EPUB Conversion CLI Usage (Markdown with Custom Directories) Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_convert.md Provides an example of explicitly specifying Markdown format along with custom input and output directories for EPUB conversion. ```bash # Explicitly specify Markdown format with custom paths python epub_convert.py --md --input /data/epubs --output /data/markdown ``` -------------------------------- ### CLI Usage Examples for epub2md Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Demonstrates various command-line invocations for epub2md, including default behavior, plain text conversion, and custom directory specifications. ```bash # Default: Markdown conversion from input/ to output/ python epub_convert.py # Convert to plain text python epub_convert.py --txt # Custom directories python epub_convert.py --input books --output markdown # Direct module execution (Markdown) python epub_to_md.py --input books --output output # Direct module execution (Plain text) python epub_to_txt.py --input books --output output ``` -------------------------------- ### Python Example: Convert Single EPUB Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Demonstrates how to use the `epub_to_text` function to convert a single EPUB file. Ensure the 'epub_to_txt' library is installed and the input EPUB file exists. ```python from epub_to_txt import epub_to_text # Convert a single EPUB file epub_to_text('books/mybook.epub', 'output/') # Creates: output/MyBook Title.txt ``` -------------------------------- ### Use Case: Convert for LLM Input Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Command-line example to convert a collection of EPUB books into plain text format suitable for Large Language Models (LLMs). ```bash python epub_convert.py --txt --input my_books --output llm_input ``` -------------------------------- ### Python Example: Batch Convert EPUBs Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Shows how to use `batch_convert_epub_to_text` to convert all EPUB files in a specified input directory to text files in an output directory. This function includes a progress indicator. ```python from epub_to_txt import batch_convert_epub_to_text # Convert all EPUB files in a directory batch_convert_epub_to_text('input_folder/', 'output_folder/') # Processes: all .epub files in input_folder/ # Output: text files in output_folder/ ``` -------------------------------- ### Use Relative Paths for Portability Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Demonstrates using relative paths for input and output directories, making the script execution more portable. Examples show usage from the project root and after changing the directory. ```bash # From project root python epub_convert.py --input ../library --output ../converted # From data directory cd data && python ../epub_convert.py --input epubs --output markdown ``` -------------------------------- ### File Monitoring Converter Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md This example uses the watchdog library to monitor a specified directory for new EPUB files. When a new EPUB file is detected, it automatically converts it to Markdown using epub_to_markdown. ```python import time from pathlib import Path from epub_to_md import epub_to_markdown from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class EPUBHandler(FileSystemEventHandler): def __init__(self, output_dir): self.output_dir = output_dir def on_created(self, event): if event.src_path.endswith('.epub'): print(f"Converting {event.src_path}...") epub_to_markdown(event.src_path, self.output_dir) # Watch directory for new EPUB files observer = Observer() output = 'converted' handler = EPUBHandler(output) observer.schedule(handler, 'watched_folder', recursive=False) observer.start() ``` -------------------------------- ### Markdown Output Format Example Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Illustrates the default Markdown output format, showing chapter headings and content separation using double newlines. ```markdown # Chapter 1 Content from chapter 1 ``` -------------------------------- ### Pattern 3: Convert to Plain Text for LLM Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Example of converting multiple EPUB files in a specified directory to plain text format, suitable for LLM processing. ```bash # Convert all EPUBs in 'library/' to plain text in 'llm_input/' python epub_convert.py --txt --input library --output llm_input ``` -------------------------------- ### Pattern 1: Convert Single Book to Markdown Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Example of converting a single EPUB file to a Markdown file using the `epub_to_markdown` function. ```python from epub_to_md import epub_to_markdown epub_to_markdown('book.epub', 'output/') # Result: output/Book Title.md ``` -------------------------------- ### Run epub_convert.py with Explicit Configuration Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/configuration.md Specify input directory, output directory, and conversion format using command-line arguments. ```bash python epub_convert.py --md --input input --output output ``` -------------------------------- ### Troubleshooting: No EPUB Files Found Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Commands to verify that the input directory exists, contains EPUB files with the correct '.epub' extension, and to try specifying an explicit path for the input directory. ```bash # Check that input directory exists and contains EPUB files ls -la input/ # Verify file extension is lowercase .epub ls -la input/*.epub # Try with explicit path python epub_convert.py --input /full/path/to/epubs --output output ``` -------------------------------- ### EPUB Conversion CLI Usage (Custom Directories) Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_convert.md Illustrates how to specify custom input and output directories for EPUB conversion using the command-line interface. This allows for flexibility in file management. ```bash # Convert with custom input and output directories python epub_convert.py --md --input my_books --output my_markdowns # Convert to text with custom directories python epub_convert.py --txt --input books --output text_files ``` -------------------------------- ### Run EPUB Converter Script Source: https://github.com/mulualem-tekle/epub2md/blob/main/readme.md Executes the main EPUB conversion script. Place EPUB files in the input directory before running. ```bash python epub_convert.py [options] ``` -------------------------------- ### Command-Line Interface Usage Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_md.md Execute the module directly from the command line to convert EPUB files. Supports specifying input and output directories. ```bash python epub_to_md.py [--input INPUT] [--output OUTPUT] ``` ```bash # Using defaults (input/ and output/) python epub_to_md.py # Custom directories python epub_to_md.py --input books --output markdown_output # Convert from specific directory python epub_to_md.py --input /path/to/epubs --output /path/to/output ``` -------------------------------- ### EPUB Conversion CLI Usage (Default Markdown) Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_convert.md Demonstrates the default command-line usage for converting EPUB files to Markdown format. This command uses the default input and output directories. ```bash # Using all defaults python epub_convert.py # Equivalent to: python epub_to_md.py --input input --output output ``` -------------------------------- ### EPUB Conversion CLI Usage (Plain Text) Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_convert.md Shows how to use the command-line interface to convert EPUB files to plain text format. The `--txt` flag is used to specify the output format. ```bash # Convert to plain text python epub_convert.py --txt # Equivalent to: python epub_to_txt.py --input input --output output ``` -------------------------------- ### Specify Custom Input and Output Directories Source: https://github.com/mulualem-tekle/epub2md/blob/main/readme.md Converts EPUB files using custom input and output directories specified by --input and --output flags. ```bash python epub_convert.py --input my_epubs --output my_markdowns ``` -------------------------------- ### Workflow: Prepare Books for LLM Analysis Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md A multi-step workflow to prepare EPUB books for LLM processing. It involves organizing files, converting them to plain text using the command-line tool, and then piping the output to an LLM processor. ```bash # Step 1: Organize your EPUB files mkdir -p library # ... place your .epub files in library/ # Step 2: Convert to plain text for LLM processing python epub_convert.py --txt --input library --output llm_input # Step 3: Feed to your LLM pipeline cat llm_input/* | python my_llm_processor.py ``` -------------------------------- ### Workflow: Create Markdown Documentation Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md A workflow for converting EPUB files into Markdown documentation. It includes placing files in an input directory, running the default conversion command, and then committing the generated Markdown files to a Git repository. ```bash # Step 1: Place EPUB files in input directory mkdir -p input # ... place your .epub files in input/ # Step 2: Convert to Markdown (default) python epub_convert.py # Step 3: Commit to documentation repository git add output/*.md git commit -m "Add converted EPUB documentation" ``` -------------------------------- ### Convert with Custom Paths Command Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md This command allows specifying custom input and output directories for the EPUB to Markdown conversion. Use `--input` for the source directory and `--output` for the destination. ```bash # Custom paths python epub_convert.py --input books --output markdown ``` -------------------------------- ### Troubleshooting: Permission Denied on Output Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Commands to ensure the output directory is writable by creating it with appropriate permissions or by specifying a temporary directory that is guaranteed to be writable. ```bash # Ensure output directory is writable mkdir -p output chmod 755 output # Or use a writable temporary directory python epub_convert.py --output /tmp/output ``` -------------------------------- ### Create Markdown and Plain Text Versions Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md This pattern demonstrates how to create both Markdown and plain text versions of the same book collection by running the conversion tool twice with different output directories and flags. ```bash # Create Markdown versions python epub_convert.py --input library --output library_markdown # Create plain text versions python epub_convert.py --txt --input library --output library_text ``` -------------------------------- ### Archive Multiple Formats Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md This script sets up directories and converts EPUB files from an input directory to both Markdown and plain text formats in separate output directories. ```bash # Setup INPUT="archive" MARKDOWN_OUT="archive_markdown" TEXT_OUT="archive_text" # Create directories mkdir -p "$INPUT" "$MARKDOWN_OUT" "$TEXT_OUT" # Convert to both formats echo "Converting to Markdown..." python epub_convert.py --input "$INPUT" --output "$MARKDOWN_OUT" echo "Converting to plain text..." python epub_convert.py --txt --input "$INPUT" --output "$TEXT_OUT" echo "All conversions complete" ``` -------------------------------- ### Pattern 2: Batch Convert Directory to Markdown Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Illustrates how to perform batch conversion of all EPUB files within a directory to Markdown format, using either the CLI or direct Python import. ```bash # Via CLI python epub_convert.py --input books --output output # Or directly python epub_to_md.py --input books --output output ``` -------------------------------- ### EPUB Conversion CLI General Usage Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_convert.md This is the general command-line usage syntax for the EPUB converter module. It outlines the available flags for format selection and directory specification. ```bash python epub_convert.py [--md | --txt] [--input INPUT] [--output OUTPUT] ``` -------------------------------- ### Pattern 4: Direct Python Import for Plain Text Batch Conversion Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Demonstrates importing and using the `batch_convert_epub_to_text` function for batch conversion of EPUB files to plain text. ```python from epub_to_txt import batch_convert_epub_to_text ``` -------------------------------- ### EPUB Converter CLI Main Function Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_convert.md This Python function sets up the argument parser for the EPUB converter CLI. It defines arguments for output format (Markdown or plain text), input directory, and output directory. ```python def main(): # Set up argument parser parser = argparse.ArgumentParser(description='Convert EPUB files to Markdown or plain text.') parser.add_argument('--md', action='store_true', help='Output in Markdown format') parser.add_argument('--txt', action='store_true', help='Output in plain text format') parser.add_argument('--input', default='input', help='Input directory containing EPUB files') parser.add_argument('--output', default='output', help='Output directory for converted files') args = parser.parse_args() # Determines script and runs conversion... ``` -------------------------------- ### Command-Line Interface Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md The epub_to_txt module can be executed directly from the command line to convert EPUB files to text. It supports specifying input and output directories. ```APIDOC ## Command-Line Interface ### Usage ```bash python epub_to_txt.py [--input INPUT] [--output OUTPUT] ``` ### Arguments #### Query Parameters - **--input** (string) - Optional - Default: 'input' - Path to directory containing EPUB files - **--output** (string) - Optional - Default: 'output' - Path to directory for output text files ### Example ```bash # Using defaults (input/ and output/) python epub_to_txt.py # Custom directories python epub_to_txt.py --input books --output text_output # Convert from specific directory python epub_to_txt.py --input /path/to/epubs --output /path/to/output ``` ``` -------------------------------- ### Plain Text Output Directory Structure Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/configuration.md Illustrates the expected directory structure for plain text output files. ```text output/ ├── Book Title 1.txt ├── Book Title 2.txt └── Book Title 3.txt ``` -------------------------------- ### Use Case: Batch Process with Python API Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Python script demonstrating how to use the `batch_convert_epub_to_markdown` function to convert EPUB files programmatically. It ensures the output directory exists before conversion. ```python from epub_to_md import batch_convert_epub_to_markdown from pathlib import Path input_dir = Path('books') output_dir = Path('markdown') output_dir.mkdir(exist_ok=True) batch_convert_epub_to_markdown(str(input_dir), str(output_dir)) ``` -------------------------------- ### Troubleshooting: Large EPUB Processing Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Instructions for processing large EPUB files by running the conversion in the background and monitoring the output file creation using 'tail -f'. ```bash # Run in background and monitor progress python epub_convert.py --input large_books --output output & tail -f output/*.md # Monitor output file creation ``` -------------------------------- ### Command-Line Interface Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_md.md The epub_to_md module can be executed directly as a command-line script for batch conversion of EPUB files to Markdown. ```APIDOC ## Command-Line Interface ### Usage ```bash python epub_to_md.py [--input INPUT] [--output OUTPUT] ``` ### Arguments #### Query Parameters - **--input** (string) - Optional - Default: 'input' - Path to directory containing EPUB files - **--output** (string) - Optional - Default: 'output' - Path to directory for output Markdown files ### Example ```bash # Using defaults (input/ and output/) python epub_to_md.py # Custom directories python epub_to_md.py --input books --output markdown_output # Convert from specific directory python epub_to_md.py --input /path/to/epubs --output /path/to/output ``` ``` -------------------------------- ### Command-Line Interface (CLI) Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Utilize the epub2md command-line tool for flexible EPUB to text conversion. Supports Markdown and plain text outputs with custom input/output directories. ```APIDOC ## Command-Line Interface (CLI) ### Description Utilize the epub2md command-line tool for flexible EPUB to text conversion. Supports Markdown and plain text outputs with custom input/output directories. ### Usage ```bash python epub_convert.py [--md | --txt] [--input INPUT] [--output OUTPUT] ``` ### Options - **`--md`** - Optional - Specifies Markdown output format (default). - **`--txt`** - Optional - Specifies plain text output format. - **`--input INPUT`** - Optional - Specifies the input directory containing EPUB files. Defaults to the current directory or a predefined input path if not specified. - **`--output OUTPUT`** - Optional - Specifies the output directory for converted files. Defaults to the current directory or a predefined output path if not specified. ### Examples - **Default: Markdown conversion from input/ to output/** ```bash python epub_convert.py ``` - **Convert to plain text**: ```bash python epub_convert.py --txt ``` - **Custom directories**: ```bash python epub_convert.py --input books --output markdown ``` - **Direct module execution (Markdown)**: ```bash python epub_to_md.py --input books --output output ``` - **Direct module execution (Plain text)**: ```bash python epub_to_txt.py --input books --output output ``` ``` -------------------------------- ### Batch Plain Text Conversion Import Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Import the `batch_convert_epub_to_text` function for converting multiple EPUB files to plain text format. ```python # Plain text from epub_to_txt import batch_convert_epub_to_text ``` -------------------------------- ### Specify Custom Input Directory Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Processes EPUB files from a directory other than the default 'input/'. Replace 'my_books' with your desired source directory. ```bash python epub_convert.py --input my_books ``` -------------------------------- ### Direct Module Execution: Plain Text Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Executes the plain text conversion module directly from the command line. Specify input and output directories as needed. ```bash # Plain text conversion directly python epub_to_txt.py [--input INPUT] [--output OUTPUT] ``` -------------------------------- ### Direct Module Execution: Markdown Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Executes the Markdown conversion module directly from the command line. Specify input and output directories as needed. ```bash # Markdown conversion directly python epub_to_md.py [--input INPUT] [--output OUTPUT] ``` -------------------------------- ### Convert EPUBs to Markdown (Default) Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md This command converts all EPUB files in the default 'input/' directory to Markdown format, saving them in the 'output/' directory. It uses default settings for output format and file naming. ```bash # Convert all EPUBs to Markdown python epub_convert.py # Result: output/Book Title.md files ``` -------------------------------- ### Batch Markdown Conversion Import Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Import the `batch_convert_epub_to_markdown` function for converting multiple EPUB files to Markdown format. ```python # Markdown from epub_to_md import batch_convert_epub_to_markdown ``` -------------------------------- ### Markdown Output Directory Structure Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/configuration.md Illustrates the expected directory structure for Markdown output files. ```text output/ ├── Book Title 1.md ├── Book Title 2.md └── Book Title 3.md ``` -------------------------------- ### epub_to_txt.py CLI Usage Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Command-line interface for the epub_to_txt module. Used for converting EPUB files to plain text format. ```bash python epub_to_txt.py [--input INPUT] [--output OUTPUT] ``` -------------------------------- ### Mixed Directory Structure (Not Recommended) Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/configuration.md Shows a mixed directory structure when both Markdown and plain text conversions are applied to the same input, which is not recommended. ```text output/ ├── Book Title 1.md ├── Book Title 2.md ├── Book Title 1.txt └── Book Title 2.txt ``` -------------------------------- ### Convert to Plain Text with Custom Paths Command Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md This command converts EPUB files to plain text format using custom input and output directories. It combines the `--txt` flag with `--input` and `--output` arguments. ```bash # Plain text with custom paths python epub_convert.py --txt --input books --output texts ``` -------------------------------- ### Process EPUB Files with Logging Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Iterates through all EPUB files in a directory, converts each to plain text, and logs the progress using Python's `logging` module. Ensures the output directory exists. ```python from pathlib import Path from epub_to_txt import epub_to_text import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) input_dir = Path('books') output_dir = Path('text') output_dir.mkdir(exist_ok=True) for epub_file in input_dir.glob('*.epub'): logger.info(f"Converting {epub_file.name}...") epub_to_text(str(epub_file), str(output_dir)) logger.info(f"Completed {epub_file.name}") ``` -------------------------------- ### Convert EPUB to Markdown (Default) Source: https://github.com/mulualem-tekle/epub2md/blob/main/readme.md Converts EPUB files to Markdown format. This is the default behavior of the script. ```bash python epub_convert.py ``` -------------------------------- ### Batch Directory Conversion to Markdown using Python Module Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Converts all EPUB files within a specified directory to Markdown format using the `batch_convert_epub_to_markdown` function. Includes a progress bar for large directories. ```python from epub_to_md import batch_convert_epub_to_markdown # Convert entire directory with progress bar batch_convert_epub_to_markdown('my_books/', 'markdown_output/') ``` -------------------------------- ### Error Handling for Batch Conversion with Directory Check Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Ensures that the input directory exists before attempting batch conversion to Markdown. Uses `pathlib.Path` for robust path handling and prints informative messages. ```python from pathlib import Path from epub_to_md import batch_convert_epub_to_markdown # Ensure directories exist input_dir = Path('books') output_dir = Path('markdown') if input_dir.exists(): batch_convert_epub_to_markdown(str(input_dir), str(output_dir)) print(f"Conversion complete. Output: {output_dir}") else: print(f"Input directory not found: {input_dir}") ``` -------------------------------- ### batch_convert_epub_to_text() Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Batch converts all EPUB files in a directory to plain text format with a progress indicator. It iterates through EPUB files in the input directory and converts each one using `epub_to_text()`. ```APIDOC ## batch_convert_epub_to_text() ### Description Batch converts all EPUB files in a directory to plain text format with a progress indicator. It iterates through EPUB files in the input directory and converts each one using `epub_to_text()`. ### Method Signature ```python def batch_convert_epub_to_text(input_dir, output_dir) ``` ### Parameters #### Path Parameters - **input_dir** (str or PathLike) - Required - Directory containing EPUB files to process - **output_dir** (str or PathLike) - Required - Directory where output text files will be saved ### Return Type `None` — Processes all EPUB files in the input directory and writes converted files to the output directory. ### Behavior - Converts `input_dir` and `output_dir` to `pathlib.Path` objects - Creates the output directory with all parent directories if they don't exist (`parents=True, exist_ok=True`) - Finds all files matching `*.epub` pattern in the input directory (non-recursive) - Uses `tqdm` to display a progress bar during conversion (desc: "Converting EPUBs", unit: "file") - Calls `epub_to_text()` for each EPUB file found - Progress bar shows file count and conversion status ### Example ```python from epub_to_txt import batch_convert_epub_to_text # Convert all EPUB files in a directory batch_convert_epub_to_text('input_folder/', 'output_folder/') # Processes: all .epub files in input_folder/ # Output: text files in output_folder/ ``` ### Example with Progress Bar Output ``` Converting EPUBs: 3/3 [100%] 00:05<00:00, 0.6 file/s ``` ``` -------------------------------- ### Batch Directory Conversion to Plain Text using Python Module Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Converts all EPUB files within a specified directory to plain text format using the `batch_convert_epub_to_text` function. This is suitable for preparing text for LLM input. ```python from epub_to_txt import batch_convert_epub_to_text # Convert entire directory for LLM input batch_convert_epub_to_text('books/', 'llm_texts/') ``` -------------------------------- ### Mixed Format Batch Conversions using Python Modules Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Processes the same library of EPUB files to generate both Markdown and plain text versions by calling separate batch conversion functions. ```python from epub_to_md import batch_convert_epub_to_markdown from epub_to_txt import batch_convert_epub_to_text # Process the same library in multiple formats batch_convert_epub_to_markdown('library/', 'markdown/') batch_convert_epub_to_text('library/', 'plaintext/') ``` -------------------------------- ### Python API: Batch Plain Text Conversion Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Uses the Python API to convert all EPUB files in an input directory to plain text in an output directory. Ensure the output directory exists. ```python # Plain text conversion from epub_to_txt import batch_convert_epub_to_text batch_convert_epub_to_text('input/', 'output/') ``` -------------------------------- ### batch_convert_epub_to_markdown() Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_md.md Batch converts all EPUB files in a directory to Markdown format with a progress indicator. It iterates through an input directory and applies the single file conversion to each EPUB found. ```APIDOC ## batch_convert_epub_to_markdown() ### Description Batch converts all EPUB files in a directory to Markdown format with a progress indicator. It iterates through an input directory and applies the single file conversion to each EPUB found. ### Parameters #### Path Parameters - **input_dir** (str or PathLike) - Required - Directory containing EPUB files to process - **output_dir** (str or PathLike) - Required - Directory where output Markdown files will be saved ### Return Type None ### Behavior - Converts `input_dir` and `output_dir` to `pathlib.Path` objects - Creates the output directory with all parent directories if they don't exist (`parents=True, exist_ok=True`) - Finds all files matching `*.epub` pattern in the input directory (non-recursive) - Uses `tqdm` to display a progress bar during conversion (desc: "Converting EPUBs", unit: "file") - Calls `epub_to_markdown()` for each EPUB file found - Progress bar shows file count and conversion status ### Example ```python from epub_to_md import batch_convert_epub_to_markdown # Convert all EPUB files in a directory batch_convert_epub_to_markdown('input_folder/', 'output_folder/') # Processes: all .epub files in input_folder/ # Output: Markdown files in output_folder/ ``` ### Example with Progress Bar Output ``` Converting EPUBs: 3/3 [100%] 00:05<00:00, 0.6 file/s ``` ``` -------------------------------- ### Convert EPUB to Plain Text Source: https://github.com/mulualem-tekle/epub2md/blob/main/readme.md Converts EPUB files to plain text format using the --txt option. ```bash python epub_convert.py --txt ``` -------------------------------- ### Single-File Plain Text Conversion Import Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Import the `epub_to_text` function for converting a single EPUB file to plain text format. ```python from epub_to_txt import epub_to_text ``` -------------------------------- ### Batch Convert EPUBs to Markdown Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_md.md Batch converts all EPUB files in a specified input directory to Markdown format, saving them to an output directory. Includes a progress indicator. ```python def batch_convert_epub_to_markdown(input_dir, output_dir): """Batch convert EPUB files in a directory to markdown files.""" ``` ```python from epub_to_md import batch_convert_epub_to_markdown # Convert all EPUB files in a directory batch_convert_epub_to_markdown('input_folder/', 'output_folder/') # Processes: all .epub files in input_folder/ # Output: Markdown files in output_folder/ ``` -------------------------------- ### epub_to_md.py CLI Usage Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Command-line interface for the epub_to_md module. Used for converting EPUB files to Markdown format. ```bash python epub_to_md.py [--input INPUT] [--output OUTPUT] ``` -------------------------------- ### Python API: Batch Markdown Conversion Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Uses the Python API to convert all EPUB files in an input directory to Markdown in an output directory. Ensure the output directory exists. ```python # Markdown conversion from epub_to_md import batch_convert_epub_to_markdown batch_convert_epub_to_markdown('input/', 'output/') ``` -------------------------------- ### Troubleshooting: Character Encoding Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Confirms that all conversions use UTF-8 encoding and provides a command to check the encoding of the output files. ```bash # Check output file encoding file output/*.md ``` -------------------------------- ### Default Markdown Conversion Command Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md This is the default command to perform EPUB to Markdown conversion. It reads from the 'input/' directory and writes to the 'output/' directory using default settings. ```bash # Default: Markdown from input/ to output/ python epub_convert.py ``` -------------------------------- ### Convert Single EPUB to Markdown using Python Module Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md This Python code snippet demonstrates how to import and use the `epub_to_markdown` function from the `epub_to_md` module to convert a single EPUB file to Markdown. It also shows how to list the contents of the output directory to verify the conversion. ```python from epub_to_md import epub_to_markdown # Convert single file epub_to_markdown('books/mybook.epub', 'output/') # Check result import os print(os.listdir('output/')) ``` -------------------------------- ### Convert Single EPUB to Markdown Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_md.md Converts a single EPUB file to Markdown format. Specify the path to the EPUB and the output directory. ```python def epub_to_markdown(epub_path, output_dir): """Convert an EPUB file to Markdown.""" ``` ```python from epub_to_md import epub_to_markdown # Convert a single EPUB file epub_to_markdown('books/mybook.epub', 'output/') # Creates: output/MyBook Title.md ``` -------------------------------- ### Specify Custom Output Directory Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Saves the converted files to a directory other than the default 'output/'. Replace 'markdown_files' with your desired destination. ```bash python epub_convert.py --output markdown_files ``` -------------------------------- ### Batch Convert EPUB to Text Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Initiates a batch conversion of EPUB files from an input directory to an output directory, saving them as plain text files. This is the primary function for LLM text preparation. ```python batch_convert_epub_to_text('my_books/', 'llm_texts/') ``` -------------------------------- ### Single File Conversion to Plain Text using Python Module Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Converts a single EPUB file to plain text format using the `epub_to_text` function from the `epub_to_txt` module. Specify the input EPUB file and the output directory. ```python from epub_to_txt import epub_to_text # Convert one file to plain text epub_to_text('books/novel.epub', 'output/') # Output file: output/Novel Title.txt ``` -------------------------------- ### Web Batch Upload Service Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md A Flask web service that accepts multiple EPUB files via POST request, converts them to Markdown using batch_convert_epub_to_markdown, and returns the converted files as a ZIP archive. ```python from flask import Flask, request, send_file from pathlib import Path from epub_to_md import batch_convert_epub_to_markdown import tempfile import zipfile app = Flask(__name__) @app.route('/convert', methods=['POST']) def convert_epubs(): # Save uploaded files upload_dir = tempfile.mkdtemp() for file in request.files.getlist('epubs'): file.save(Path(upload_dir) / file.filename) # Convert output_dir = tempfile.mkdtemp() batch_convert_epub_to_markdown(upload_dir, output_dir) # Return as ZIP return send_file( create_zip(output_dir), mimetype='application/zip', download_name='converted.zip' ) ``` -------------------------------- ### Convert EPUBs to Plain Text for LLM Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md This command converts EPUB files to plain text format, suitable for LLM input. It specifies custom input and output directories and uses a single newline as the content separator. The output files will have a .txt extension. ```bash # Convert for LLM processing python epub_convert.py --txt --input books --output texts # Result: output/Book Title.txt files with single-line separation ``` -------------------------------- ### Single-File Markdown Conversion Import Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Import the `epub_to_markdown` function for converting a single EPUB file to Markdown format. ```python # Or single-file versions from epub_to_md import epub_to_markdown ``` -------------------------------- ### Selective Conversion with Scripting Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md This bash script processes only EPUB files matching a specific pattern (e.g., 'fiction_*.epub') within an input directory and converts them to Markdown in a specified output directory. ```bash #!/bin/bash # Process specific EPUB files matching a pattern INPUT_DIR="books" OUTPUT_DIR="converted" # Create output directory mkdir -p "$OUTPUT_DIR" # Process only files matching pattern for epub_file in "$INPUT_DIR"/fiction_*.epub; do if [ -f "$epub_file" ]; then echo "Processing $(basename "$epub_file")..." python epub_to_md.py --input "$(dirname "$epub_file")" --output "$OUTPUT_DIR" fi done ``` -------------------------------- ### Direct Function Import - Plain Text Conversion Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Import and use Python functions for converting EPUB files to plain text format. Supports single file or batch directory conversion. ```APIDOC ## Direct Function Import - Plain Text Conversion ### Description Import and use Python functions for converting EPUB files to plain text format. Supports single file or batch directory conversion. ### Functions - **`epub_to_text(epub_path, output_dir)`** - Converts a single EPUB file to plain text. - **Parameters**: - `epub_path` (string) - Required - Path to the input EPUB file. - `output_dir` (string) - Required - Directory where the plain text file will be saved. - **Usage Example**: ```python from epub_to_txt import epub_to_text epub_to_text('book.epub', 'output/') # Result: output/Book Title.txt ``` - **`batch_convert_epub_to_text(input_dir, output_dir)`** - Converts all EPUB files in a directory to plain text. - **Parameters**: - `input_dir` (string) - Required - Directory containing EPUB files. - `output_dir` (string) - Required - Directory where the converted plain text files will be saved. - **Usage Example**: ```python from epub_to_txt import batch_convert_epub_to_text batch_convert_epub_to_text('input/', 'output/') ``` ``` -------------------------------- ### Direct Python Import for Markdown Conversion Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Import and use functions for Markdown conversion directly within Python scripts. Supports single file and batch directory conversions. ```python # Markdown conversion from epub_to_md import epub_to_markdown, batch_convert_epub_to_markdown # Plain text conversion from epub_to_txt import epub_to_text, batch_convert_epub_to_text # Convert single file epub_to_markdown('book.epub', 'output/') # Convert entire directory batch_convert_epub_to_markdown('input/', 'output/') ``` -------------------------------- ### epub_to_markdown() Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_md.md Converts a single EPUB file to Markdown format. It extracts text from XHTML content and saves it as a .md file. ```APIDOC ## epub_to_markdown() ### Description Converts a single EPUB file to Markdown format. It extracts text from XHTML content and saves it as a .md file. ### Parameters #### Path Parameters - **epub_path** (str or PathLike) - Required - Path to the EPUB file to convert - **output_dir** (str or PathLike) - Required - Directory where the output Markdown file will be saved ### Return Type None ### Behavior - Reads the EPUB file using `ebooklib.epub.read_epub()` - Extracts the book title from EPUB metadata (DC.title field) or uses the EPUB filename as fallback - Iterates through all items in the EPUB book - Processes only items with media type `application/xhtml+xml` (XHTML content) - Parses HTML content using BeautifulSoup and extracts plain text - Joins all extracted text sections with double newlines (`\n\n`) - Writes the combined content to `{title}.md` file in the output directory - Creates the output directory if it doesn't exist ### Example ```python from epub_to_md import epub_to_markdown # Convert a single EPUB file epub_to_markdown('books/mybook.epub', 'output/') # Creates: output/MyBook Title.md ``` ``` -------------------------------- ### Direct Function Import - Markdown Conversion Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/overview.md Import and use Python functions for converting EPUB files to Markdown format. Supports single file or batch directory conversion. ```APIDOC ## Direct Function Import - Markdown Conversion ### Description Import and use Python functions for converting EPUB files to Markdown format. Supports single file or batch directory conversion. ### Functions - **`epub_to_markdown(epub_path, output_dir)`** - Converts a single EPUB file to Markdown. - **Parameters**: - `epub_path` (string) - Required - Path to the input EPUB file. - `output_dir` (string) - Required - Directory where the Markdown file will be saved. - **Usage Example**: ```python from epub_to_md import epub_to_markdown epub_to_markdown('book.epub', 'output/') # Result: output/Book Title.md ``` - **`batch_convert_epub_to_markdown(input_dir, output_dir)`** - Converts all EPUB files in a directory to Markdown. - **Parameters**: - `input_dir` (string) - Required - Directory containing EPUB files. - `output_dir` (string) - Required - Directory where the converted Markdown files will be saved. - **Usage Example**: ```python from epub_to_md import batch_convert_epub_to_markdown batch_convert_epub_to_markdown('input/', 'output/') ``` ``` -------------------------------- ### Archive Processing for LLM Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Converts an entire book collection to plain text for LLM processing. It reads from a specified input directory and saves to an output directory, displaying progress. ```bash python epub_convert.py --txt --input /media/books --output /data/llm_input ``` -------------------------------- ### API: Batch Convert EPUBs to Markdown Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Python function to convert all EPUB files within a directory to Markdown format. The converted files will be saved in the specified output directory. ```python # Convert all EPUB files in directory batch_convert_epub_to_markdown(input_dir: str, output_dir: str) → None ``` -------------------------------- ### Python Function Signature: batch_convert_epub_to_text Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Defines the function signature for batch converting EPUB files. It takes the input directory containing EPUBs and the output directory for text files. ```python def batch_convert_epub_to_text(input_dir, output_dir): """Batch convert EPUB files in a directory to text files.""" ``` -------------------------------- ### Handle Nested Paths with Absolute Paths Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Uses absolute paths for both input and output directories to ensure reliable operation across different directory structures and script locations. ```bash python epub_convert.py --input /home/user/books/epub --output /home/user/books/markdown ``` -------------------------------- ### Plain Text Conversion Functions Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md These functions are responsible for converting EPUB files into plain text format. They support both single file conversion and batch processing of directories. ```APIDOC ## epub_to_text ### Description Converts a single EPUB file to plain text format. ### Method `epub_to_text(epub_path: str, output_dir: str)` ### Parameters #### Path Parameters - **epub_path** (str) - Required - The path to the EPUB file to convert. - **output_dir** (str) - Required - The directory where the plain text file will be saved. ### Response #### Success Response - None (The function saves the output to the specified directory). ## batch_convert_epub_to_text ### Description Converts all EPUB files within a specified input directory to plain text format and saves them to an output directory. ### Method `batch_convert_epub_to_text(input_dir: str, output_dir: str)` ### Parameters #### Path Parameters - **input_dir** (str) - Required - The directory containing the EPUB files to convert. - **output_dir** (str) - Required - The directory where the converted plain text files will be saved. ``` -------------------------------- ### Convert UTF-8 to ISO-8859-1 Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Use the `iconv` command to convert a file from UTF-8 to ISO-8859-1 encoding. This is useful when compatibility with systems expecting specific encodings is required. ```bash iconv -f UTF-8 -t ISO-8859-1 output/file.md ``` -------------------------------- ### API: Convert Single EPUB to Markdown Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Python function to convert a single EPUB file to Markdown format. The output will be placed in the specified directory. ```python from epub_to_md import epub_to_markdown # Convert single EPUB file epub_to_markdown(epub_path: str, output_dir: str) → None ``` -------------------------------- ### API: Batch Convert EPUBs to Plain Text Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Python function to convert all EPUB files within a directory to plain text format. The converted files will be saved in the specified output directory. ```python # Convert all EPUB files in directory batch_convert_epub_to_text(input_dir: str, output_dir: str) → None ``` -------------------------------- ### Markdown Conversion Functions Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md These functions handle the conversion of EPUB files to Markdown format. They include options for converting a single file or batch processing an entire directory. ```APIDOC ## epub_to_markdown ### Description Converts a single EPUB file to Markdown format. ### Method `epub_to_markdown(epub_path: str, output_dir: str)` ### Parameters #### Path Parameters - **epub_path** (str) - Required - The path to the EPUB file to convert. - **output_dir** (str) - Required - The directory where the Markdown file will be saved. ### Response #### Success Response - None (The function saves the output to the specified directory). ## batch_convert_epub_to_markdown ### Description Converts all EPUB files within a specified input directory to Markdown format and saves them to an output directory. ### Method `batch_convert_epub_to_markdown(input_dir: str, output_dir: str)` ### Parameters #### Path Parameters - **input_dir** (str) - Required - The directory containing the EPUB files to convert. - **output_dir** (str) - Required - The directory where the converted Markdown files will be saved. ``` -------------------------------- ### epub_to_text() Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Converts a single EPUB file to plain text format. It extracts text content from XHTML files within the EPUB and saves it as a .txt file. ```APIDOC ## epub_to_text() ### Description Converts a single EPUB file to plain text format. It extracts text content from XHTML files within the EPUB and saves it as a .txt file. ### Method Signature ```python def epub_to_text(epub_path, output_dir) ``` ### Parameters #### Path Parameters - **epub_path** (str or PathLike) - Required - Path to the EPUB file to convert - **output_dir** (str or PathLike) - Required - Directory where the output text file will be saved ### Return Type `None` — Writes the converted output to a file in the specified output directory. ### Behavior - Reads the EPUB file using `ebooklib.epub.read_epub()` - Extracts the book title from EPUB metadata (DC.title field) or uses the EPUB filename as fallback - Iterates through all items in the EPUB book - Processes only items with media type `application/xhtml+xml` (XHTML content) - Parses HTML content using BeautifulSoup and extracts plain text - Joins all extracted text sections with single newlines (`\n`) - Writes the combined content to `{title}.txt` file in the output directory - Creates the output directory if it doesn't exist ### Example ```python from epub_to_txt import epub_to_text # Convert a single EPUB file epub_to_text('books/mybook.epub', 'output/') # Creates: output/MyBook Title.txt ``` ``` -------------------------------- ### Explicitly Specify Markdown Format Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/usage-examples.md Explicitly sets the output format to Markdown using the --md flag. This is functionally equivalent to the default behavior. ```bash python epub_convert.py --md --input books --output output ``` -------------------------------- ### API: Convert Single EPUB to Plain Text Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/README.md Python function to convert a single EPUB file to plain text format. The output will be placed in the specified directory. ```python from epub_to_txt import epub_to_text # Convert single EPUB file epub_to_text(epub_path: str, output_dir: str) → None ``` -------------------------------- ### Python Function Signature: epub_to_text Source: https://github.com/mulualem-tekle/epub2md/blob/main/_autodocs/api-reference/epub_to_txt.md Defines the function signature for converting a single EPUB file to plain text. It takes the EPUB file path and the output directory as arguments. ```python def epub_to_text(epub_path, output_dir): """Convert an EPUB file to plain text.""" ```