Add --logo option to embed a brand logo on the title page
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>main
parent
0eac070dac
commit
5909a3ac9b
|
|
@ -18,7 +18,7 @@ from docx import Document
|
||||||
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
||||||
from docx.oxml import OxmlElement
|
from docx.oxml import OxmlElement
|
||||||
from docx.oxml.ns import qn
|
from docx.oxml.ns import qn
|
||||||
from docx.shared import Pt
|
from docx.shared import Inches, Pt
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
@ -159,10 +159,15 @@ def _md_to_doc(doc, content):
|
||||||
# Document assembly
|
# Document assembly
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def build_docx(md_dir: Path, output: Path, title: str):
|
def build_docx(md_dir: Path, output: Path, title: str, logo: Path | None = None):
|
||||||
doc = Document()
|
doc = Document()
|
||||||
|
|
||||||
# Title page
|
# Title page
|
||||||
|
if logo is not None:
|
||||||
|
logo_para = doc.add_paragraph()
|
||||||
|
logo_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||||
|
logo_para.add_run().add_picture(str(logo), width=Inches(2))
|
||||||
|
|
||||||
heading = doc.add_heading(title, level=0)
|
heading = doc.add_heading(title, level=0)
|
||||||
heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
heading.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||||
date_para = doc.add_paragraph(f'Generated: {datetime.now().strftime("%Y-%m-%d")}')
|
date_para = doc.add_paragraph(f'Generated: {datetime.now().strftime("%Y-%m-%d")}')
|
||||||
|
|
@ -216,13 +221,20 @@ def main():
|
||||||
parser.add_argument('--dir', '-d', default='.', help='Directory containing .md files (default: .)')
|
parser.add_argument('--dir', '-d', default='.', help='Directory containing .md files (default: .)')
|
||||||
parser.add_argument('--output', '-o', default='documentation.docx', help='Output .docx path')
|
parser.add_argument('--output', '-o', default='documentation.docx', help='Output .docx path')
|
||||||
parser.add_argument('--title', '-t', default='Project Documentation', help='Document title')
|
parser.add_argument('--title', '-t', default='Project Documentation', help='Document title')
|
||||||
|
parser.add_argument('--logo', '-l', default=None, help='Path to logo image (PNG/JPG) for the title page')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
md_dir = Path(args.dir).resolve()
|
md_dir = Path(args.dir).resolve()
|
||||||
if not md_dir.is_dir():
|
if not md_dir.is_dir():
|
||||||
sys.exit(f'Error: {md_dir} is not a directory')
|
sys.exit(f'Error: {md_dir} is not a directory')
|
||||||
|
|
||||||
build_docx(md_dir, Path(args.output), args.title)
|
logo_path = None
|
||||||
|
if args.logo:
|
||||||
|
logo_path = Path(args.logo).resolve()
|
||||||
|
if not logo_path.is_file():
|
||||||
|
sys.exit(f'Error: logo file not found: {logo_path}')
|
||||||
|
|
||||||
|
build_docx(md_dir, Path(args.output), args.title, logo=logo_path)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue