first commit

This commit is contained in:
2026-05-21 08:40:24 -04:00
commit b084545275
711 changed files with 3659856 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
## Easy Word Docs in Python
---
This library implements a word document builder class that allows you to pass a list of functions and it will generate a word document using each function to generate a page.
This has the benefit of providing modularity in scripts that make word documents as their outputs as a script can be configured to use any combination
of page functions to build a word doc.
### Tips:
---
The current page number can be accessed by accessing the current_section member variable of the document builder.
The current figure number and table number can be accessed with the member variables figure_number and table_number. This allows you to have dynanmically
labeled graphs and tables in your document pages
The PageConfig class allows you to customize the page breaking behavior to ensure the correct number of pages are present in the final document
## Usage:
---
```
from docx import Document
from docx.shared import Inches, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from typing import Callable, List, Any
# Example page function
def title_page(builder: WordDocumentBuilder, title: str = "Document Title", author: str = "Author", **kwargs):
"""Create a title page."""
title_para = builder.doc.add_paragraph()
title_run = title_para.add_run(title)
title_run.font.size = Pt(28)
title_run.bold = True
title_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
builder.doc.add_paragraph() # Spacing
author_para = builder.doc.add_paragraph(author)
author_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
# Create document builder
builder = WordDocumentBuilder()
# Define pages, these are functions
pages = [
PageConfig(title_page, add_page_break=False)
]
# Create document with custom parameters. Only pages and the output path are required, the following parameters will be passed into the functions that
# request them through **kwargs. Think of it kinda like dependancy injection.
doc = builder.create_document(
pages,
"output/my_document.docx",
title="My Custom Report",
author="John Doe",
heading="Introduction",
content="This is the introduction section with detailed information.",
table_data=[
["Product", "Price", "Quantity"],
["Widget", "$10", "100"],
["Gadget", "$25", "50"]
],
table_title="Sales Data"
)
```

View File

@@ -0,0 +1,4 @@
Metadata-Version: 2.4
Name: pasbdc_word_library
Version: 0.1.0
Summary: A tool used to make the generation of word documents in python a little easier.

View File

@@ -0,0 +1,9 @@
README.md
pyproject.toml
pasbdc_word_library/__init__.py
pasbdc_word_library/doclibrary.py
pasbdc_word_library/theme_helpers.py
pasbdc_word_library.egg-info/PKG-INFO
pasbdc_word_library.egg-info/SOURCES.txt
pasbdc_word_library.egg-info/dependency_links.txt
pasbdc_word_library.egg-info/top_level.txt

View File

@@ -0,0 +1 @@
pasbdc_word_library

View File

@@ -0,0 +1,4 @@
# libs/word_library/__init__.py
from .doclibrary import WordDocumentBuilder, PageConfig, title_page, content_page, table_page, image_page
from .theme_helpers import theme_paragraph, theme_title
__all__ = ['WordDocumentBuilder', 'PageConfig', 'title_page', 'content_page', 'table_page', 'image_page', 'theme_paragraph', 'theme_title']

View File

@@ -0,0 +1,127 @@
from docx import Document
from docx.shared import Inches, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from typing import Callable, List, Any
import os
from dataclasses import dataclass
@dataclass
class PageConfig:
page_function: Callable
add_page_break: bool = True
class WordDocumentBuilder:
"""Build Word documents using configurable page functions."""
def __init__(self):
self.doc = Document()
self.current_section = 1
self.figure_number = 0;
self.table_number = 0;
def add_page_break(self):
"""Add a page break to the document."""
self.doc.add_page_break()
def create_document(self, page_functions: List[PageConfig], output_path: str, **kwargs):
"""
Create a Word document by executing page functions.
Args:
page_functions: List of functions that add content to the document
output_path: Path where the document will be saved
**kwargs: Additional parameters passed to all page functions
Returns:
The created Document object
"""
for i, page_conf in enumerate(page_functions):
# Execute page function with builder and any additional kwargs
page_conf.page_function(self, **kwargs)
# Add page break after each page except the last
if i < len(page_functions) - 1 and page_conf.add_page_break:
self.add_page_break()
# Increment the section state
self.current_section += 1
# Ensure directory exists
os.makedirs(os.path.dirname(output_path) if os.path.dirname(output_path) else '.', exist_ok=True)
# Save document
self.doc.save(output_path)
return self.doc
# Example page functions
def title_page(builder: WordDocumentBuilder, title: str = "Document Title", author: str = "Author", **kwargs):
"""Create a title page."""
title_para = builder.doc.add_paragraph()
title_run = title_para.add_run(title)
title_run.font.size = Pt(28)
title_run.bold = True
title_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
builder.doc.add_paragraph() # Spacing
author_para = builder.doc.add_paragraph(author)
author_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
def content_page(builder: WordDocumentBuilder, heading: str = "Section", content: str = "", **kwargs):
"""Create a content page with heading and body text."""
builder.doc.add_heading(heading, level=1)
builder.doc.add_paragraph(content)
def table_page(builder: WordDocumentBuilder, table_data: List[List[str]] = [], table_title: str = "Data Table", **kwargs):
"""Create a page with a table."""
if table_data is None:
table_data = [["Header 1", "Header 2"], ["Data 1", "Data 2"]]
builder.doc.add_heading(table_title, level=1)
table = builder.doc.add_table(rows=len(table_data), cols=len(table_data[0]))
table.style = 'Light Grid Accent 1'
for i, row in enumerate(table_data):
for j, cell_value in enumerate(row):
table.rows[i].cells[j].text = str(cell_value)
def image_page(builder: WordDocumentBuilder, image_path: str = "", caption: str = "", **kwargs):
"""Create a page with an image."""
if image_path and os.path.exists(image_path):
builder.doc.add_paragraph(caption)
builder.doc.add_picture(image_path, width=Inches(5))
# Usage example
if __name__ == "__main__":
# Create document builder
builder = WordDocumentBuilder()
# Define pages
pages = [
PageConfig(title_page, add_page_break=True),
PageConfig(content_page, add_page_break=True),
PageConfig(table_page, add_page_break=False),
]
# Create document with custom parameters
doc = builder.create_document(
pages,
"output/my_document.docx",
title="My Custom Report",
author="John Doe",
heading="Introduction",
content="This is the introduction section with detailed information.",
table_data=[
["Product", "Price", "Quantity"],
["Widget", "$10", "100"],
["Gadget", "$25", "50"]
],
table_title="Sales Data"
)
print("Document created successfully!")

View File

@@ -0,0 +1,22 @@
import docx
from docx.shared import RGBColor, Pt
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
def theme_paragraph(
paragraph,
font_size_pt:int=9,
font_name:str="Futera",
color:RGBColor=RGBColor(15,27,38)
):
for run in paragraph.runs:
run.font.name = font_name
run.font.size = Pt(font_size_pt)
run.font.color.rgb = color
def theme_title(title_run):
title_run.bold = True
title_run.font.name = 'Futera'
title_run.font.size = Pt(12)
title_run.font.color.rgb = RGBColor(113, 191, 68)
title_run.alignment = WD_ALIGN_PARAGRAPH.LEFT

View File

@@ -0,0 +1,12 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "pasbdc_word_library"
version = "0.1.0"
description = "A tool used to make the generation of word documents in python a little easier."
[tool.setuptools]
packages = ["pasbdc_word_library"]