alirezarezvani/claude-skills / engineering/book-to-skill/skills/book-to-skill
book-to-skill
UnverifiedUse when turning a book, docs folder or spec collection (PDF, EPUB, DOCX, HTML, Markdown) into an agent skill, before summarizing it by hand. Extracts with extract_document.py, compiles chapters, glossary and cheatsheet, validates with book_skill_validator.py and enforces a rights gate.
scripts/book_to_skill/parsers/docx.py
View on GitHubfrom __future__ import annotations
import zipfile
import sys
from book_to_skill.exceptions import ExtractionError
from book_to_skill.zip_safety import safe_read, validate_zip_xml_safety
def extract_docx_with_python_docx(docx_path: str) -> str | None:
try:
import docx
document = docx.Document(docx_path)
parts = [paragraph.text for paragraph in document.paragraphs if paragraph.text]
for table in document.tables:
for row in table.rows:
cells = [cell.text.strip() for cell in row.cells]
if any(cells):
parts.append("\t".join(cells))
return "\n".join(parts)
except ImportError:
return None
except Exception as e:
print(f" [warn] extract_docx_with_python_docx failed: {type(e).__name__}: {e}", file=sys.stderr)
return None
def extract_docx_with_zipfile(docx_path: str) -> str | None:
try:
import xml.etree.ElementTree as ET
with zipfile.ZipFile(docx_path) as zf:
xml_bytes = safe_read(zf, "word/document.xml")
root = ET.fromstring(xml_bytes)
ns = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
parts: list[str] = []
def emit_block(elem) -> None:
# Walk block content in document order. Paragraphs join their runs;
# tables emit one tab-joined line per row (same row format as the
# python-docx path, but order-preserving — python-docx appends all
# tables last). Unknown wrappers (e.g. <w:sdt> content controls) are
# recursed into so their paragraphs/tables are not lost; <w:p> and
# <w:tbl> are NOT recursed into, so table-cell paragraphs are not
# double-counted. Cell text concatenates the cell's runs; nested
# tables fold into the parent cell and are also emitted standalone
# (rare; best-effort).
for child in elem:
tag = child.tag
if tag == f"{ns}p":
texts = [t.text for t in child.iter(f"{ns}t") if t.text]
if texts:
parts.append("".join(texts))
elif tag == f"{ns}tbl":
for row in child.iter(f"{ns}tr"):
cells = []
for cell in row.iter(f"{ns}tc"):
cell_texts = [t.text for t in cell.iter(f"{ns}t") if t.text]
cells.append("".join(cell_texts).strip())
if any(cells):
parts.append("\t".join(cells))
else:
emit_block(child)
body = root.find(f"{ns}body")
emit_block(body if body is not None else root)
return "\n".join(parts) if parts else None
except Exception as e:
print(f" [warn] extract_docx_with_zipfile failed: {type(e).__name__}: {e}", file=sys.stderr)
return None
def validate_docx_xml_safety(docx_path: str) -> None:
"""Refuse a DOCX whose XML declares a DTD or entity (billion-laughs / XXE).
Now a thin wrapper over the shared zip guard so EPUB gets identical
treatment; the scan also enforces per-member size and compression-ratio caps
before decompressing anything.
"""
validate_zip_xml_safety(docx_path, label="DOCX")
def extract_docx(docx_path: str) -> tuple[str, str]:
validate_docx_xml_safety(docx_path)
print("Trying python-docx...", end=" ", flush=True)
text = extract_docx_with_python_docx(docx_path)
if text and text.strip():
print("OK")
return text, "python-docx"
print("not available")
print("Trying stdlib DOCX parser...", end=" ", flush=True)
text = extract_docx_with_zipfile(docx_path)
if text and text.strip():
print("OK")
return text, "zipfile-docx"
print("FAILED")
raise ExtractionError(
"Could not extract text from DOCX.\n"
"Install python-docx for best results:\n"
" pip3 install python-docx"
)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| canvas-designanthropics/skillsCreate beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| docxanthropics/skillsUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| pdfanthropics/skillsUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| pptxanthropics/skillsUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or presentations; reading, parsing, or extracting text from any .pptx or .potx file (even if the extracted content will be used elsewhere, like in an email or summary); editing, modifying, or updating existing presentations; combining or splitting slide files; working with templates (.potx), layouts, speaker notes, or comments. Trigger whenever the user mentions "deck," "slides," "presentation," or references a .pptx or .potx filename, regardless of what they plan to do with the content afterward. If a .pptx or .potx file needs to be opened, created, or touched, use this skill. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |