Working version for ebook rendering!!

This commit is contained in:
2025-11-04 12:57:15 +01:00
parent fdb3023919
commit de18b1c2cc
8 changed files with 583 additions and 292 deletions
+137 -48
View File
@@ -2,7 +2,7 @@
"""
Simple EPUB page renderer tool.
This tool uses the pyWebLayout epub_reader and typesetting modules to:
This tool uses the pyWebLayout epub_reader and layout modules to:
1. Load an EPUB file
2. Render the first X pages according to command line arguments
3. Save the pages as PNG images
@@ -15,17 +15,19 @@ import os
import sys
import argparse
from pathlib import Path
from typing import Optional
from typing import Optional, List
# Add the parent directory to sys.path to import pyWebLayout
sys.path.insert(0, str(Path(__file__).parent.parent))
try:
from pyWebLayout.io.readers.epub_reader import read_epub
from pyWebLayout.layout.document_pagination import DocumentPaginator
from pyWebLayout.layout.ereader_layout import BidirectionalLayouter, RenderingPosition
from pyWebLayout.layout.document_layouter import paragraph_layouter
from pyWebLayout.concrete.page import Page
from pyWebLayout.style.page_style import PageStyle
from pyWebLayout.style.fonts import Font
from pyWebLayout.style.layout import Alignment
from pyWebLayout.abstract.block import Block
from PIL import Image, ImageDraw
except ImportError as e:
print(f"Error importing required modules: {e}")
@@ -50,14 +52,14 @@ def render_page_to_image(page: Page) -> Image.Image:
return rendered_image
else:
# If render() doesn't return a PIL Image, create error image
error_image = Image.new('RGB', page._size, 'white')
error_image = Image.new('RGB', page.size, 'white')
draw = ImageDraw.Draw(error_image)
draw.text((20, 20), "Error: Page.render() did not return PIL Image", fill='red')
return error_image
except Exception as e:
# Create error image if rendering fails
error_image = Image.new('RGB', page._size, 'white')
error_image = Image.new('RGB', page.size, 'white')
draw = ImageDraw.Draw(error_image)
draw.text((20, 20), f"Rendering error: {str(e)}", fill='red')
print(f"Warning: Error rendering page: {e}")
@@ -85,19 +87,25 @@ def extract_text_from_page(page: Page) -> str:
# Import abstract block types
from pyWebLayout.abstract.block import Paragraph, Heading, HList, Table, Image as AbstractImage
from pyWebLayout.concrete.text import Line
# Handle abstract block objects first
if isinstance(element, Paragraph):
# Extract text from paragraph
# Handle Line objects (concrete)
if isinstance(element, Line):
line_text = []
if hasattr(element, '_text_objects') and element._text_objects:
for text_obj in element._text_objects:
if hasattr(text_obj, 'text'):
line_text.append(str(text_obj.text))
if line_text:
text_lines.append(f"{indent}{' '.join(line_text)}")
# Handle abstract block objects
elif isinstance(element, (Paragraph, Heading)):
# Extract text from paragraph/heading
paragraph_text = extract_text_from_paragraph(element)
if paragraph_text:
text_lines.append(f"{indent}PARAGRAPH: {paragraph_text}")
elif isinstance(element, Heading):
# Extract text from heading
heading_text = extract_text_from_paragraph(element)
if heading_text:
text_lines.append(f"{indent}HEADING: {heading_text}")
block_type = "HEADING" if isinstance(element, Heading) else "PARAGRAPH"
text_lines.append(f"{indent}{block_type}: {paragraph_text}")
elif isinstance(element, HList):
text_lines.append(f"{indent}LIST:")
@@ -115,7 +123,7 @@ def extract_text_from_page(page: Page) -> str:
elif isinstance(element, AbstractImage):
alt_text = getattr(element, 'alt_text', '')
src = getattr(element, 'src', 'Unknown')
src = getattr(element, 'source', 'Unknown')
text_lines.append(f"{indent}[IMAGE: {alt_text or src}]")
# Handle containers with children
@@ -129,15 +137,6 @@ def extract_text_from_page(page: Page) -> str:
if text:
text_lines.append(f"{indent}{text}")
# Handle lines with text objects
elif hasattr(element, '_text_objects') and element._text_objects:
line_text = []
for text_obj in element._text_objects:
if hasattr(text_obj, 'text'):
line_text.append(str(text_obj.text))
if line_text:
text_lines.append(f"{indent}{' '.join(line_text)}")
# Handle other object types by showing their class name
else:
class_name = element.__class__.__name__
@@ -148,8 +147,8 @@ def extract_text_from_page(page: Page) -> str:
words = []
try:
# Try to get words from the paragraph
if hasattr(para_obj, 'words') and callable(para_obj.words):
for _, word in para_obj.words():
if hasattr(para_obj, 'words_iter') and callable(para_obj.words_iter):
for _, word in para_obj.words_iter():
if hasattr(word, 'text'):
words.append(word.text)
else:
@@ -183,6 +182,27 @@ def extract_text_from_page(page: Page) -> str:
return "\n".join(text_lines)
def get_all_blocks_from_book(book) -> List[Block]:
"""
Extract all blocks from all chapters in the book.
Args:
book: The Book object from epub_reader
Returns:
List of all Block objects
"""
all_blocks = []
# Iterate through all chapters
for chapter in book.chapters:
# Get blocks from the chapter
if hasattr(chapter, '_blocks'):
all_blocks.extend(chapter._blocks)
return all_blocks
def main():
"""Main function to handle command line arguments and process the EPUB."""
parser = argparse.ArgumentParser(
@@ -234,6 +254,13 @@ Examples:
help='Page margin in pixels (default: 40)'
)
parser.add_argument(
'--align', '-a',
choices=['left', 'justify'],
default='left',
help='Text alignment: left or justify (default: left)'
)
args = parser.parse_args()
# Validate arguments
@@ -268,42 +295,100 @@ Examples:
except Exception as e:
print(f"Error loading EPUB file: {e}")
import traceback
traceback.print_exc()
return 1
# Set up pagination
page_size = (args.width, args.height)
margins = (args.margin, args.margin, args.margin, args.margin) # top, right, bottom, left
# Extract all blocks from the book
print("Extracting content blocks...")
try:
all_blocks = get_all_blocks_from_book(book)
print(f"Extracted {len(all_blocks)} content blocks")
if not all_blocks:
print("No content blocks found in EPUB. The book might be empty.")
return 1
# Apply alignment setting to all paragraphs and headings
from pyWebLayout.abstract.block import Paragraph, Heading
from pyWebLayout.style.alignment import Alignment
alignment = Alignment.JUSTIFY if args.align == 'justify' else Alignment.LEFT
print(f"Applying {args.align} alignment to all text blocks...")
# Note: We'll pass alignment to the layouter which will handle it during rendering
# The alignment is applied at the Line level in paragraph_layouter
except Exception as e:
print(f"Error extracting blocks: {e}")
import traceback
traceback.print_exc()
return 1
print(f"Setting up pagination with page size {page_size} and margins {margins}")
# Set up page style and layouter
page_size = (args.width, args.height)
page_style = PageStyle(
background_color=(255, 255, 255),
border_width=args.margin,
border_color=(200, 200, 200),
padding=(10, 10, 10, 10), # top, right, bottom, left
line_spacing=5,
inter_block_spacing=15
)
print(f"Setting up layouter with page size {page_size} and {args.align} alignment")
try:
paginator = DocumentPaginator(
document=book,
layouter = BidirectionalLayouter(
blocks=all_blocks,
page_style=page_style,
page_size=page_size,
margins=margins,
spacing=5,
halign=Alignment.LEFT
alignment_override=alignment
)
except Exception as e:
print(f"Error setting up paginator: {e}")
print(f"Error setting up layouter: {e}")
import traceback
traceback.print_exc()
return 1
# Render pages
print(f"Rendering {args.pages} pages...")
print(f"Rendering up to {args.pages} pages...")
try:
# Generate pages
pages = paginator.paginate(max_pages=args.pages)
pages = []
current_position = RenderingPosition() # Start from beginning
for page_num in range(args.pages):
print(f"Rendering page {page_num + 1}/{args.pages}...")
try:
# Render the page
page, next_position = layouter.render_page_forward(current_position)
pages.append(page)
# Check if we've reached the end of the document
if next_position.block_index >= len(all_blocks):
print(f"Reached end of document after {page_num + 1} pages")
break
# Update position for next page
current_position = next_position
except Exception as e:
print(f"Error rendering page {page_num + 1}: {e}")
import traceback
traceback.print_exc()
break
if not pages:
print("No pages were generated. The book might be empty or there might be an issue with pagination.")
print("No pages were generated.")
return 1
print(f"Generated {len(pages)} pages")
# Render each page to an image and extract text
# Save each page to an image and extract text
for i, page in enumerate(pages):
print(f"Rendering page {i + 1}/{len(pages)}...")
print(f"Saving page {i + 1}/{len(pages)}...")
try:
# Create image from page using pyWebLayout's built-in rendering
@@ -324,18 +409,22 @@ Examples:
print(f"Saved: {output_path} and {text_path}")
except Exception as e:
print(f"Error rendering page {i + 1}: {e}")
print(f"Error saving page {i + 1}: {e}")
import traceback
traceback.print_exc()
continue
print(f"\nCompleted! Rendered {len(pages)} pages to {args.output_dir}")
# Show pagination progress
if hasattr(paginator, 'get_progress'):
progress = paginator.get_progress() * 100
# Calculate progress through the book
if len(all_blocks) > 0:
progress = (current_position.block_index / len(all_blocks)) * 100
print(f"Progress through book: {progress:.1f}%")
except Exception as e:
print(f"Error during pagination/rendering: {e}")
import traceback
traceback.print_exc()
return 1
return 0