This commit is contained in:
@@ -1,329 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Comprehensive demo of the EbookReader functionality.
|
||||
|
||||
This script demonstrates all features of the pyWebLayout EbookReader:
|
||||
- Loading EPUB files
|
||||
- Page navigation (forward/backward)
|
||||
- Position saving/loading
|
||||
- Chapter navigation
|
||||
- Font size and spacing adjustments
|
||||
- Getting book and position information
|
||||
|
||||
Usage:
|
||||
python ereader_demo.py path/to/book.epub
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Add parent directory to path to import pyWebLayout
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from pyWebLayout.layout.ereader_application import EbookReader
|
||||
|
||||
|
||||
def print_separator():
|
||||
"""Print a visual separator."""
|
||||
print("\n" + "="*70 + "\n")
|
||||
|
||||
|
||||
def demo_basic_navigation(reader: EbookReader):
|
||||
"""Demonstrate basic page navigation."""
|
||||
print("DEMO: Basic Navigation")
|
||||
print_separator()
|
||||
|
||||
# Get current page
|
||||
print("Getting first page...")
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
print(f"✓ Current page rendered: {page.size}")
|
||||
reader.render_to_file("demo_page_001.png")
|
||||
print(" Saved to: demo_page_001.png")
|
||||
|
||||
# Navigate forward
|
||||
print("\nNavigating to next page...")
|
||||
page = reader.next_page()
|
||||
if page:
|
||||
print(f"✓ Next page rendered: {page.size}")
|
||||
reader.render_to_file("demo_page_002.png")
|
||||
print(" Saved to: demo_page_002.png")
|
||||
|
||||
# Navigate backward
|
||||
print("\nNavigating to previous page...")
|
||||
page = reader.previous_page()
|
||||
if page:
|
||||
print(f"✓ Previous page rendered: {page.size}")
|
||||
|
||||
print_separator()
|
||||
|
||||
|
||||
def demo_position_management(reader: EbookReader):
|
||||
"""Demonstrate position save/load functionality."""
|
||||
print("DEMO: Position Management")
|
||||
print_separator()
|
||||
|
||||
# Navigate a few pages forward
|
||||
print("Navigating forward 3 pages...")
|
||||
for i in range(3):
|
||||
reader.next_page()
|
||||
|
||||
# Save position
|
||||
print("Saving current position as 'demo_bookmark'...")
|
||||
success = reader.save_position("demo_bookmark")
|
||||
if success:
|
||||
print("✓ Position saved successfully")
|
||||
|
||||
# Get position info
|
||||
pos_info = reader.get_position_info()
|
||||
print(f"\nCurrent position info:")
|
||||
print(f" Chapter: {pos_info.get('chapter', {}).get('title', 'N/A')}")
|
||||
print(f" Block index: {pos_info['position']['block_index']}")
|
||||
print(f" Word index: {pos_info['position']['word_index']}")
|
||||
print(f" Progress: {pos_info['progress']*100:.1f}%")
|
||||
|
||||
# Navigate away
|
||||
print("\nNavigating forward 5 more pages...")
|
||||
for i in range(5):
|
||||
reader.next_page()
|
||||
|
||||
# Load saved position
|
||||
print("Loading saved position 'demo_bookmark'...")
|
||||
page = reader.load_position("demo_bookmark")
|
||||
if page:
|
||||
print("✓ Position restored successfully")
|
||||
reader.render_to_file("demo_restored_position.png")
|
||||
print(" Saved to: demo_restored_position.png")
|
||||
|
||||
# List all saved positions
|
||||
positions = reader.list_saved_positions()
|
||||
print(f"\nAll saved positions: {positions}")
|
||||
|
||||
print_separator()
|
||||
|
||||
|
||||
def demo_chapter_navigation(reader: EbookReader):
|
||||
"""Demonstrate chapter navigation."""
|
||||
print("DEMO: Chapter Navigation")
|
||||
print_separator()
|
||||
|
||||
# Get all chapters
|
||||
chapters = reader.get_chapters()
|
||||
print(f"Found {len(chapters)} chapters:")
|
||||
for title, idx in chapters[:5]: # Show first 5
|
||||
print(f" [{idx}] {title}")
|
||||
|
||||
if len(chapters) > 5:
|
||||
print(f" ... and {len(chapters) - 5} more")
|
||||
|
||||
# Jump to a chapter by index
|
||||
if len(chapters) > 1:
|
||||
print(f"\nJumping to chapter 1...")
|
||||
page = reader.jump_to_chapter(1)
|
||||
if page:
|
||||
print("✓ Jumped to chapter successfully")
|
||||
reader.render_to_file("demo_chapter_1.png")
|
||||
print(" Saved to: demo_chapter_1.png")
|
||||
|
||||
# Get current chapter info
|
||||
chapter_info = reader.get_current_chapter_info()
|
||||
if chapter_info:
|
||||
print(f" Current chapter: {chapter_info['title']}")
|
||||
|
||||
# Jump to a chapter by title (if we have chapters)
|
||||
if len(chapters) > 0:
|
||||
first_chapter_title = chapters[0][0]
|
||||
print(f"\nJumping to chapter by title: '{first_chapter_title}'...")
|
||||
page = reader.jump_to_chapter(first_chapter_title)
|
||||
if page:
|
||||
print("✓ Jumped to chapter by title successfully")
|
||||
|
||||
print_separator()
|
||||
|
||||
|
||||
def demo_font_size_adjustment(reader: EbookReader):
|
||||
"""Demonstrate font size adjustments."""
|
||||
print("DEMO: Font Size Adjustment")
|
||||
print_separator()
|
||||
|
||||
# Save current page for comparison
|
||||
print("Rendering page at normal font size (1.0x)...")
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
reader.render_to_file("demo_font_normal.png")
|
||||
print("✓ Saved to: demo_font_normal.png")
|
||||
|
||||
# Increase font size
|
||||
print("\nIncreasing font size...")
|
||||
page = reader.increase_font_size()
|
||||
if page:
|
||||
print(f"✓ Font size increased to {reader.get_font_size():.1f}x")
|
||||
reader.render_to_file("demo_font_larger.png")
|
||||
print(" Saved to: demo_font_larger.png")
|
||||
|
||||
# Increase again
|
||||
print("\nIncreasing font size again...")
|
||||
page = reader.increase_font_size()
|
||||
if page:
|
||||
print(f"✓ Font size increased to {reader.get_font_size():.1f}x")
|
||||
reader.render_to_file("demo_font_largest.png")
|
||||
print(" Saved to: demo_font_largest.png")
|
||||
|
||||
# Decrease font size
|
||||
print("\nDecreasing font size...")
|
||||
page = reader.decrease_font_size()
|
||||
if page:
|
||||
print(f"✓ Font size decreased to {reader.get_font_size():.1f}x")
|
||||
|
||||
# Set specific font size
|
||||
print("\nResetting to normal font size (1.0x)...")
|
||||
page = reader.set_font_size(1.0)
|
||||
if page:
|
||||
print("✓ Font size reset to 1.0x")
|
||||
|
||||
print_separator()
|
||||
|
||||
|
||||
def demo_spacing_adjustment(reader: EbookReader):
|
||||
"""Demonstrate line and block spacing adjustments."""
|
||||
print("DEMO: Spacing Adjustment")
|
||||
print_separator()
|
||||
|
||||
# Save current page
|
||||
print("Rendering page with default spacing...")
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
reader.render_to_file("demo_spacing_default.png")
|
||||
print("✓ Saved to: demo_spacing_default.png")
|
||||
|
||||
# Increase line spacing
|
||||
print("\nIncreasing line spacing to 10px...")
|
||||
page = reader.set_line_spacing(10)
|
||||
if page:
|
||||
print("✓ Line spacing increased")
|
||||
reader.render_to_file("demo_spacing_lines_10.png")
|
||||
print(" Saved to: demo_spacing_lines_10.png")
|
||||
|
||||
# Increase inter-block spacing
|
||||
print("\nIncreasing inter-block spacing to 25px...")
|
||||
page = reader.set_inter_block_spacing(25)
|
||||
if page:
|
||||
print("✓ Inter-block spacing increased")
|
||||
reader.render_to_file("demo_spacing_blocks_25.png")
|
||||
print(" Saved to: demo_spacing_blocks_25.png")
|
||||
|
||||
# Reset to defaults
|
||||
print("\nResetting spacing to defaults (line: 5px, block: 15px)...")
|
||||
reader.set_line_spacing(5)
|
||||
page = reader.set_inter_block_spacing(15)
|
||||
if page:
|
||||
print("✓ Spacing reset to defaults")
|
||||
|
||||
print_separator()
|
||||
|
||||
|
||||
def demo_book_information(reader: EbookReader):
|
||||
"""Demonstrate getting book information."""
|
||||
print("DEMO: Book Information")
|
||||
print_separator()
|
||||
|
||||
# Get book info
|
||||
book_info = reader.get_book_info()
|
||||
print("Book Information:")
|
||||
print(f" Title: {book_info['title']}")
|
||||
print(f" Author: {book_info['author']}")
|
||||
print(f" Document ID: {book_info['document_id']}")
|
||||
print(f" Total blocks: {book_info['total_blocks']}")
|
||||
print(f" Total chapters: {book_info['total_chapters']}")
|
||||
print(f" Page size: {book_info['page_size']}")
|
||||
print(f" Font scale: {book_info['font_scale']}")
|
||||
|
||||
# Get reading progress
|
||||
progress = reader.get_reading_progress()
|
||||
print(f"\nReading Progress: {progress*100:.1f}%")
|
||||
|
||||
# Get detailed position info
|
||||
pos_info = reader.get_position_info()
|
||||
print("\nDetailed Position:")
|
||||
print(f" Chapter index: {pos_info['position']['chapter_index']}")
|
||||
print(f" Block index: {pos_info['position']['block_index']}")
|
||||
print(f" Word index: {pos_info['position']['word_index']}")
|
||||
|
||||
chapter = pos_info.get('chapter', {})
|
||||
if chapter.get('title'):
|
||||
print(f" Current chapter: {chapter['title']}")
|
||||
|
||||
print_separator()
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to run all demos."""
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python ereader_demo.py path/to/book.epub")
|
||||
print("\nExample EPUBs to try:")
|
||||
print(" - tests/data/test.epub")
|
||||
print(" - tests/data/test2.epub")
|
||||
sys.exit(1)
|
||||
|
||||
epub_path = sys.argv[1]
|
||||
|
||||
if not os.path.exists(epub_path):
|
||||
print(f"Error: File not found: {epub_path}")
|
||||
sys.exit(1)
|
||||
|
||||
print("="*70)
|
||||
print(" EbookReader Demo - pyWebLayout")
|
||||
print("="*70)
|
||||
print(f"\nLoading EPUB: {epub_path}")
|
||||
|
||||
# Create reader with context manager
|
||||
with EbookReader(page_size=(800, 1000)) as reader:
|
||||
# Load the EPUB
|
||||
if not reader.load_epub(epub_path):
|
||||
print("Error: Failed to load EPUB file")
|
||||
sys.exit(1)
|
||||
|
||||
print("✓ EPUB loaded successfully")
|
||||
|
||||
# Run all demos
|
||||
try:
|
||||
demo_basic_navigation(reader)
|
||||
demo_position_management(reader)
|
||||
demo_chapter_navigation(reader)
|
||||
demo_font_size_adjustment(reader)
|
||||
demo_spacing_adjustment(reader)
|
||||
demo_book_information(reader)
|
||||
|
||||
print("\n" + "="*70)
|
||||
print(" Demo Complete!")
|
||||
print("="*70)
|
||||
print("\nGenerated demo images:")
|
||||
demo_files = [
|
||||
"demo_page_001.png",
|
||||
"demo_page_002.png",
|
||||
"demo_restored_position.png",
|
||||
"demo_chapter_1.png",
|
||||
"demo_font_normal.png",
|
||||
"demo_font_larger.png",
|
||||
"demo_font_largest.png",
|
||||
"demo_spacing_default.png",
|
||||
"demo_spacing_lines_10.png",
|
||||
"demo_spacing_blocks_25.png"
|
||||
]
|
||||
|
||||
for filename in demo_files:
|
||||
if os.path.exists(filename):
|
||||
print(f" ✓ {filename}")
|
||||
|
||||
print("\nAll features demonstrated successfully!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\nError during demo: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,289 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Generate animated GIFs demonstrating EbookReader functionality.
|
||||
|
||||
This script creates animated GIFs showcasing:
|
||||
1. Page navigation (next/previous)
|
||||
2. Font size adjustment
|
||||
3. Chapter navigation
|
||||
4. Bookmark/position management
|
||||
|
||||
The GIFs are saved to the examples/ directory and can be included in documentation.
|
||||
|
||||
Usage:
|
||||
python generate_ereader_gifs.py path/to/book.epub [output_dir]
|
||||
|
||||
Example:
|
||||
python generate_ereader_gifs.py ../tests/data/test.epub ../docs/images
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
# Add parent directory to path to import pyWebLayout
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from pyWebLayout.layout.ereader_application import EbookReader
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def create_gif(images: List[Image.Image], output_path: str, duration: int = 800, loop: int = 0):
|
||||
"""
|
||||
Create an animated GIF from a list of PIL Images.
|
||||
|
||||
Args:
|
||||
images: List of PIL Images to animate
|
||||
output_path: Path where to save the GIF
|
||||
duration: Duration of each frame in milliseconds
|
||||
loop: Number of loops (0 = infinite)
|
||||
"""
|
||||
if not images:
|
||||
print(f"Warning: No images provided for {output_path}")
|
||||
return False
|
||||
|
||||
try:
|
||||
# Save as animated GIF
|
||||
images[0].save(
|
||||
output_path,
|
||||
save_all=True,
|
||||
append_images=images[1:],
|
||||
duration=duration,
|
||||
loop=loop,
|
||||
optimize=False
|
||||
)
|
||||
print(f"✓ Created: {output_path} ({len(images)} frames)")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"✗ Error creating {output_path}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def generate_page_navigation_gif(reader: EbookReader, output_path: str):
|
||||
"""Generate GIF showing page navigation (forward and backward)."""
|
||||
print("\n[1/4] Generating page navigation GIF...")
|
||||
|
||||
frames = []
|
||||
|
||||
# Go to beginning
|
||||
reader.set_font_size(1.0)
|
||||
|
||||
# Capture 5 pages going forward
|
||||
for i in range(5):
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
reader.next_page()
|
||||
|
||||
# Go back to start
|
||||
for _ in range(4):
|
||||
reader.previous_page()
|
||||
|
||||
# Capture 5 pages going forward again (smoother loop)
|
||||
for i in range(5):
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
reader.next_page()
|
||||
|
||||
create_gif(frames, output_path, duration=600)
|
||||
|
||||
|
||||
def generate_font_size_gif(reader: EbookReader, output_path: str):
|
||||
"""Generate GIF showing font size adjustment."""
|
||||
print("\n[2/4] Generating font size adjustment GIF...")
|
||||
|
||||
frames = []
|
||||
|
||||
# Reset to beginning and normal font
|
||||
for _ in range(10):
|
||||
reader.previous_page()
|
||||
reader.set_font_size(1.0)
|
||||
|
||||
# Font sizes to demonstrate
|
||||
font_scales = [0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.3, 1.2, 1.1, 1.0, 0.9, 0.8]
|
||||
|
||||
for scale in font_scales:
|
||||
page = reader.set_font_size(scale)
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
|
||||
# Reset to normal
|
||||
reader.set_font_size(1.0)
|
||||
|
||||
create_gif(frames, output_path, duration=500)
|
||||
|
||||
|
||||
def generate_chapter_navigation_gif(reader: EbookReader, output_path: str):
|
||||
"""Generate GIF showing chapter navigation."""
|
||||
print("\n[3/4] Generating chapter navigation GIF...")
|
||||
|
||||
frames = []
|
||||
|
||||
# Reset font
|
||||
reader.set_font_size(1.0)
|
||||
|
||||
# Get chapters
|
||||
chapters = reader.get_chapters()
|
||||
|
||||
if len(chapters) == 0:
|
||||
print(" Warning: No chapters found, skipping chapter navigation GIF")
|
||||
return
|
||||
|
||||
# Visit first few chapters (or loop through available chapters)
|
||||
chapter_indices = list(range(min(5, len(chapters))))
|
||||
|
||||
# Add some chapters twice for smoother animation
|
||||
for idx in chapter_indices:
|
||||
page = reader.jump_to_chapter(idx)
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
# Add a second frame at each chapter for pause effect
|
||||
frames.append(page.copy())
|
||||
|
||||
# Go back to first chapter
|
||||
page = reader.jump_to_chapter(0)
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
|
||||
if frames:
|
||||
create_gif(frames, output_path, duration=1000)
|
||||
else:
|
||||
print(" Warning: No frames captured for chapter navigation")
|
||||
|
||||
|
||||
def generate_bookmark_gif(reader: EbookReader, output_path: str):
|
||||
"""Generate GIF showing bookmark save/load functionality."""
|
||||
print("\n[4/4] Generating bookmark/position GIF...")
|
||||
|
||||
frames = []
|
||||
|
||||
# Reset font
|
||||
reader.set_font_size(1.0)
|
||||
|
||||
# Go to beginning
|
||||
for _ in range(20):
|
||||
reader.previous_page()
|
||||
|
||||
# Capture initial position
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
frames.append(page.copy()) # Hold frame
|
||||
|
||||
# Navigate forward a bit
|
||||
for i in range(3):
|
||||
reader.next_page()
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
|
||||
# Save this position
|
||||
reader.save_position("demo_bookmark")
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
frames.append(page.copy()) # Hold frame to show saved position
|
||||
|
||||
# Navigate away
|
||||
for i in range(5):
|
||||
reader.next_page()
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
|
||||
# Hold at distant position
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
frames.append(page.copy())
|
||||
|
||||
# Jump back to bookmark
|
||||
page = reader.load_position("demo_bookmark")
|
||||
if page:
|
||||
frames.append(page.copy())
|
||||
frames.append(page.copy())
|
||||
frames.append(page.copy()) # Hold longer to show we're back
|
||||
|
||||
create_gif(frames, output_path, duration=600)
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to generate all GIFs."""
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python generate_ereader_gifs.py path/to/book.epub [output_dir]")
|
||||
print("\nExample:")
|
||||
print(" python generate_ereader_gifs.py ../tests/data/test.epub ../docs/images")
|
||||
sys.exit(1)
|
||||
|
||||
epub_path = sys.argv[1]
|
||||
output_dir = sys.argv[2] if len(sys.argv) > 2 else "."
|
||||
|
||||
# Validate EPUB path
|
||||
if not os.path.exists(epub_path):
|
||||
print(f"Error: EPUB file not found: {epub_path}")
|
||||
sys.exit(1)
|
||||
|
||||
# Create output directory
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
print("="*70)
|
||||
print(" EbookReader Animated GIF Generator")
|
||||
print("="*70)
|
||||
print(f"\nInput EPUB: {epub_path}")
|
||||
print(f"Output directory: {output_dir}")
|
||||
|
||||
# Create paths for output GIFs
|
||||
nav_gif = os.path.join(output_dir, "ereader_page_navigation.gif")
|
||||
font_gif = os.path.join(output_dir, "ereader_font_size.gif")
|
||||
chapter_gif = os.path.join(output_dir, "ereader_chapter_navigation.gif")
|
||||
bookmark_gif = os.path.join(output_dir, "ereader_bookmarks.gif")
|
||||
|
||||
try:
|
||||
# Create reader
|
||||
with EbookReader(page_size=(600, 800), margin=30) as reader:
|
||||
# Load EPUB
|
||||
print("\nLoading EPUB...")
|
||||
if not reader.load_epub(epub_path):
|
||||
print("Error: Failed to load EPUB file")
|
||||
sys.exit(1)
|
||||
|
||||
print("✓ EPUB loaded successfully")
|
||||
|
||||
# Get book info
|
||||
book_info = reader.get_book_info()
|
||||
print(f"\nBook: {book_info['title']}")
|
||||
print(f"Author: {book_info['author']}")
|
||||
print(f"Chapters: {book_info['total_chapters']}")
|
||||
print(f"Blocks: {book_info['total_blocks']}")
|
||||
|
||||
print("\nGenerating GIFs...")
|
||||
print("-" * 70)
|
||||
|
||||
# Generate all GIFs
|
||||
generate_page_navigation_gif(reader, nav_gif)
|
||||
generate_font_size_gif(reader, font_gif)
|
||||
generate_chapter_navigation_gif(reader, chapter_gif)
|
||||
generate_bookmark_gif(reader, bookmark_gif)
|
||||
|
||||
print("\n" + "="*70)
|
||||
print(" Generation Complete!")
|
||||
print("="*70)
|
||||
print("\nGenerated files:")
|
||||
for gif_path in [nav_gif, font_gif, chapter_gif, bookmark_gif]:
|
||||
if os.path.exists(gif_path):
|
||||
size = os.path.getsize(gif_path)
|
||||
print(f" ✓ {gif_path} ({size/1024:.1f} KB)")
|
||||
|
||||
print("\nYou can now add these GIFs to your README.md!")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\nError: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,96 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple example showing the most common EbookReader usage.
|
||||
|
||||
This script loads an EPUB and allows you to navigate through it,
|
||||
saving each page as an image.
|
||||
|
||||
Usage:
|
||||
python simple_ereader_example.py book.epub
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add parent directory to path to import pyWebLayout
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
from pyWebLayout.layout.ereader_application import EbookReader
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python simple_ereader_example.py book.epub")
|
||||
sys.exit(1)
|
||||
|
||||
epub_path = sys.argv[1]
|
||||
|
||||
# Create reader and load EPUB
|
||||
print(f"Loading: {epub_path}")
|
||||
reader = EbookReader(page_size=(800, 1000))
|
||||
|
||||
if not reader.load_epub(epub_path):
|
||||
print("Failed to load EPUB")
|
||||
sys.exit(1)
|
||||
|
||||
# Get book information
|
||||
info = reader.get_book_info()
|
||||
print(f"\nBook: {info['title']}")
|
||||
print(f"Author: {info['author']}")
|
||||
print(f"Total blocks: {info['total_blocks']}")
|
||||
|
||||
# Get chapters
|
||||
chapters = reader.get_chapters()
|
||||
print(f"Chapters: {len(chapters)}")
|
||||
if chapters:
|
||||
print("\nChapter list:")
|
||||
for title, idx in chapters[:10]: # Show first 10
|
||||
print(f" {idx}: {title}")
|
||||
if len(chapters) > 10:
|
||||
print(f" ... and {len(chapters) - 10} more")
|
||||
|
||||
# Navigate through first 10 pages
|
||||
print("\nRendering first 10 pages...")
|
||||
for i in range(10):
|
||||
page = reader.get_current_page()
|
||||
if page:
|
||||
filename = f"page_{i+1:03d}.png"
|
||||
reader.render_to_file(filename)
|
||||
|
||||
# Show progress
|
||||
progress = reader.get_reading_progress()
|
||||
chapter_info = reader.get_current_chapter_info()
|
||||
chapter_name = chapter_info['title'] if chapter_info else "N/A"
|
||||
|
||||
print(f" Page {i+1}: {filename} (Progress: {progress*100:.1f}%, Chapter: {chapter_name})")
|
||||
|
||||
# Move to next page
|
||||
if not reader.next_page():
|
||||
print(" Reached end of book")
|
||||
break
|
||||
|
||||
# Save current position
|
||||
reader.save_position("stopped_at_page_10")
|
||||
print("\nSaved position as 'stopped_at_page_10'")
|
||||
|
||||
# Example: Jump to a chapter (if available)
|
||||
if len(chapters) >= 2:
|
||||
print(f"\nJumping to chapter: {chapters[1][0]}")
|
||||
reader.jump_to_chapter(1)
|
||||
reader.render_to_file("chapter_2_start.png")
|
||||
print(" Saved to: chapter_2_start.png")
|
||||
|
||||
# Example: Increase font size
|
||||
print("\nIncreasing font size...")
|
||||
reader.increase_font_size()
|
||||
reader.render_to_file("larger_font.png")
|
||||
print(f" Font size now: {reader.get_font_size():.1f}x")
|
||||
print(" Saved to: larger_font.png")
|
||||
|
||||
# Close reader (saves current position automatically)
|
||||
reader.close()
|
||||
print("\nDone! Current position saved automatically.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user