116 lines
3.4 KiB
Python
116 lines
3.4 KiB
Python
# sumaexcel/styles.py
|
|
from typing import Dict, Any, Optional, Union
|
|
from openpyxl.styles import (
|
|
Font, PatternFill, Alignment, Border, Side,
|
|
NamedStyle, Protection, Color
|
|
)
|
|
from openpyxl.styles.differential import DifferentialStyle
|
|
from openpyxl.formatting.rule import Rule
|
|
from openpyxl.worksheet.worksheet import Worksheet
|
|
|
|
class StyleManager:
|
|
"""Excel style management class"""
|
|
|
|
@staticmethod
|
|
def create_font(
|
|
name: str = "Arial",
|
|
size: int = 11,
|
|
bold: bool = False,
|
|
italic: bool = False,
|
|
color: str = "000000",
|
|
underline: str = None,
|
|
strike: bool = False
|
|
) -> Font:
|
|
"""Create a Font object with specified parameters"""
|
|
return Font(
|
|
name=name,
|
|
size=size,
|
|
bold=bold,
|
|
italic=italic,
|
|
color=color,
|
|
underline=underline,
|
|
strike=strike
|
|
)
|
|
|
|
@staticmethod
|
|
def create_fill(
|
|
fill_type: str = "solid",
|
|
start_color: str = "FFFFFF",
|
|
end_color: str = None
|
|
) -> PatternFill:
|
|
"""Create a PatternFill object"""
|
|
return PatternFill(
|
|
fill_type=fill_type,
|
|
start_color=start_color,
|
|
end_color=end_color or start_color
|
|
)
|
|
|
|
@staticmethod
|
|
def create_border(
|
|
style: str = "thin",
|
|
color: str = "000000"
|
|
) -> Border:
|
|
"""Create a Border object"""
|
|
side = Side(style=style, color=color)
|
|
return Border(
|
|
left=side,
|
|
right=side,
|
|
top=side,
|
|
bottom=side
|
|
)
|
|
|
|
@staticmethod
|
|
def create_alignment(
|
|
horizontal: str = "general",
|
|
vertical: str = "bottom",
|
|
wrap_text: bool = False,
|
|
shrink_to_fit: bool = False,
|
|
indent: int = 0
|
|
) -> Alignment:
|
|
"""Create an Alignment object"""
|
|
return Alignment(
|
|
horizontal=horizontal,
|
|
vertical=vertical,
|
|
wrap_text=wrap_text,
|
|
shrink_to_fit=shrink_to_fit,
|
|
indent=indent
|
|
)
|
|
|
|
@staticmethod
|
|
def copy_style(source_cell: Any, target_cell: Any) -> None:
|
|
"""Copy all style properties from source cell to target cell"""
|
|
target_cell.font = Font(
|
|
name=source_cell.font.name,
|
|
size=source_cell.font.size,
|
|
bold=source_cell.font.bold,
|
|
italic=source_cell.font.italic,
|
|
color=source_cell.font.color,
|
|
underline=source_cell.font.underline,
|
|
strike=source_cell.font.strike
|
|
)
|
|
|
|
if source_cell.fill.patternType != None:
|
|
target_cell.fill = PatternFill(
|
|
fill_type=source_cell.fill.patternType,
|
|
start_color=source_cell.fill.start_color.rgb,
|
|
end_color=source_cell.fill.end_color.rgb
|
|
)
|
|
|
|
target_cell.border = Border(
|
|
left=source_cell.border.left,
|
|
right=source_cell.border.right,
|
|
top=source_cell.border.top,
|
|
bottom=source_cell.border.bottom
|
|
)
|
|
|
|
target_cell.alignment = Alignment(
|
|
horizontal=source_cell.alignment.horizontal,
|
|
vertical=source_cell.alignment.vertical,
|
|
wrap_text=source_cell.alignment.wrap_text,
|
|
shrink_to_fit=source_cell.alignment.shrink_to_fit,
|
|
indent=source_cell.alignment.indent
|
|
)
|
|
|
|
if source_cell.number_format:
|
|
target_cell.number_format = source_cell.number_format
|