Color specification and visual formatting including fills, lines, shadows, and effects. Supports RGB, theme colors, transparency, and comprehensive visual styling options.
Create and work with colors using different color models and sources.
class RGBColor:
"""RGB color specification with red, green, blue values."""
def __init__(self, r: int, g: int, b: int):
"""
Create RGB color.
Parameters:
- r: Red component (0-255)
- g: Green component (0-255)
- b: Blue component (0-255)
"""
@classmethod
def from_string(cls, rgb_hex_str: str) -> 'RGBColor':
"""
Create RGB color from hex string.
Parameters:
- rgb_hex_str: Hex color string like 'FF0000' or '#FF0000'
Returns:
RGBColor object
"""
def __str__(self) -> str:
"""Hex string representation like 'FF0000'."""
class ColorFormat:
"""Color formatting with type-specific properties."""
@property
def type(self) -> MSO_COLOR_TYPE:
"""Color type (RGB, SCHEME, etc.)."""
@property
def rgb(self) -> RGBColor:
"""RGB color when type is RGB."""
@rgb.setter
def rgb(self, rgb: RGBColor):
"""Set RGB color."""
@property
def theme_color(self) -> MSO_THEME_COLOR_INDEX:
"""Theme color when type is SCHEME."""
@theme_color.setter
def theme_color(self, theme_color: MSO_THEME_COLOR_INDEX):
"""Set theme color."""
@property
def brightness(self) -> float:
"""Brightness adjustment (-1.0 to 1.0)."""
@brightness.setter
def brightness(self, brightness: float):
"""Set brightness adjustment."""Configure shape and text background fills including solid colors, gradients, patterns, and images.
class FillFormat:
"""Fill formatting for shapes and backgrounds."""
@property
def type(self) -> MSO_FILL_TYPE:
"""Fill type (SOLID, GRADIENT, PATTERNED, etc.)."""
def solid(self):
"""Set fill to solid color type."""
def gradient(self):
"""Set fill to gradient type."""
def patterned(self):
"""Set fill to pattern type."""
def background(self):
"""Set fill to background (transparent)."""
@property
def fore_color(self) -> ColorFormat:
"""Foreground color for solid/gradient/pattern fills."""
@property
def back_color(self) -> ColorFormat:
"""Background color for gradient/pattern fills."""
@property
def gradient_angle(self) -> float:
"""Gradient direction angle in degrees."""
@gradient_angle.setter
def gradient_angle(self, angle: float):
"""Set gradient angle."""
@property
def gradient_stops(self) -> list:
"""List of gradient color stops."""
@property
def pattern(self) -> MSO_PATTERN_TYPE:
"""Pattern type for patterned fills."""
@pattern.setter
def pattern(self, pattern: MSO_PATTERN_TYPE):
"""Set pattern type."""Configure borders, outlines, and connector lines with colors, widths, and dash patterns.
class LineFormat:
"""Line and border formatting."""
@property
def color(self) -> ColorFormat:
"""Line color."""
@property
def width(self) -> int:
"""Line width in EMU."""
@width.setter
def width(self, width: int):
"""Set line width."""
@property
def dash_style(self) -> MSO_LINE_DASH_STYLE:
"""Line dash pattern."""
@dash_style.setter
def dash_style(self, dash_style: MSO_LINE_DASH_STYLE):
"""Set dash pattern."""
@property
def fill(self) -> FillFormat:
"""Line fill (for complex line effects)."""Add and configure shadow effects for shapes and text.
class ShadowFormat:
"""Shadow effect formatting."""
@property
def inherit(self) -> bool:
"""True if shadow inherits from style hierarchy."""
@inherit.setter
def inherit(self, inherit: bool):
"""Set shadow inheritance."""
@property
def visible(self) -> bool:
"""True if shadow is visible."""
@visible.setter
def visible(self, visible: bool):
"""Set shadow visibility."""
@property
def style(self) -> MSO_SHADOW_STYLE:
"""Shadow style type."""
@style.setter
def style(self, style: MSO_SHADOW_STYLE):
"""Set shadow style."""
@property
def blur_radius(self) -> int:
"""Shadow blur radius in EMU."""
@blur_radius.setter
def blur_radius(self, radius: int):
"""Set blur radius."""
@property
def distance(self) -> int:
"""Shadow offset distance in EMU."""
@distance.setter
def distance(self, distance: int):
"""Set shadow distance."""
@property
def angle(self) -> float:
"""Shadow angle in degrees."""
@angle.setter
def angle(self, angle: float):
"""Set shadow angle."""
@property
def color(self) -> ColorFormat:
"""Shadow color."""
@property
def transparency(self) -> float:
"""Shadow transparency (0.0-1.0)."""
@transparency.setter
def transparency(self, transparency: float):
"""Set shadow transparency."""from pptx.enum.dml import MSO_COLOR_TYPE, MSO_THEME_COLOR_INDEX
# Color types
MSO_COLOR_TYPE.RGB # RGB color
MSO_COLOR_TYPE.SCHEME # Theme color
MSO_COLOR_TYPE.HSL # HSL color
MSO_COLOR_TYPE.PRESET # Preset color
MSO_COLOR_TYPE.SCRGB # sRGB color
MSO_COLOR_TYPE.SYSTEM # System color
# Theme color indices
MSO_THEME_COLOR_INDEX.ACCENT_1
MSO_THEME_COLOR_INDEX.ACCENT_2
MSO_THEME_COLOR_INDEX.ACCENT_3
MSO_THEME_COLOR_INDEX.ACCENT_4
MSO_THEME_COLOR_INDEX.ACCENT_5
MSO_THEME_COLOR_INDEX.ACCENT_6
MSO_THEME_COLOR_INDEX.DARK_1
MSO_THEME_COLOR_INDEX.DARK_2
MSO_THEME_COLOR_INDEX.LIGHT_1
MSO_THEME_COLOR_INDEX.LIGHT_2
MSO_THEME_COLOR_INDEX.HYPERLINK
MSO_THEME_COLOR_INDEX.FOLLOWED_HYPERLINKfrom pptx.enum.dml import MSO_FILL_TYPE, MSO_PATTERN_TYPE
# Fill types
MSO_FILL_TYPE.SOLID # Solid color fill
MSO_FILL_TYPE.GRADIENT # Gradient fill
MSO_FILL_TYPE.PATTERNED # Pattern fill
MSO_FILL_TYPE.TEXTURED # Texture fill
MSO_FILL_TYPE.PICTURE # Picture fill
MSO_FILL_TYPE.BACKGROUND # Transparent/background
# Pattern types
MSO_PATTERN_TYPE.CROSS
MSO_PATTERN_TYPE.DIAGONAL_BRICK
MSO_PATTERN_TYPE.DIAGONAL_CROSS
MSO_PATTERN_TYPE.DIVOT
MSO_PATTERN_TYPE.DOTTED_DIAMOND
MSO_PATTERN_TYPE.DOTTED_GRID
MSO_PATTERN_TYPE.HORIZONTAL_BRICK
MSO_PATTERN_TYPE.LARGE_CHECKER_BOARD
MSO_PATTERN_TYPE.LARGE_CONFETTI
MSO_PATTERN_TYPE.LARGE_GRID
MSO_PATTERN_TYPE.LIGHT_DOWNWARD_DIAGONAL
MSO_PATTERN_TYPE.LIGHT_HORIZONTAL
MSO_PATTERN_TYPE.LIGHT_UPWARD_DIAGONAL
MSO_PATTERN_TYPE.LIGHT_VERTICAL
MSO_PATTERN_TYPE.NARROW_HORIZONTAL
MSO_PATTERN_TYPE.NARROW_VERTICAL
MSO_PATTERN_TYPE.OUTLINED_DIAMOND
MSO_PATTERN_TYPE.PLAID
MSO_PATTERN_TYPE.SHINGLE
MSO_PATTERN_TYPE.SMALL_CHECKER_BOARD
MSO_PATTERN_TYPE.SMALL_CONFETTI
MSO_PATTERN_TYPE.SMALL_GRID
MSO_PATTERN_TYPE.SOLID_DIAMOND
MSO_PATTERN_TYPE.SPHERE
MSO_PATTERN_TYPE.TRELLIS
MSO_PATTERN_TYPE.VERTICAL
MSO_PATTERN_TYPE.WAVE
MSO_PATTERN_TYPE.WEAVE
MSO_PATTERN_TYPE.WIDE_DOWNWARD_DIAGONAL
MSO_PATTERN_TYPE.WIDE_UPWARD_DIAGONAL
MSO_PATTERN_TYPE.ZIG_ZAGfrom pptx.enum.dml import MSO_LINE_DASH_STYLE
# Line dash styles
MSO_LINE_DASH_STYLE.SOLID # Solid line
MSO_LINE_DASH_STYLE.SQUARE_DOT # Square dots
MSO_LINE_DASH_STYLE.ROUND_DOT # Round dots
MSO_LINE_DASH_STYLE.DASH # Dashes
MSO_LINE_DASH_STYLE.DASH_DOT # Alternating dashes and dots
MSO_LINE_DASH_STYLE.DASH_DOT_DOT # Dash followed by two dots
MSO_LINE_DASH_STYLE.LONG_DASH # Long dashes
MSO_LINE_DASH_STYLE.LONG_DASH_DOT # Long dash followed by dot
MSO_LINE_DASH_STYLE.LONG_DASH_DOT_DOT # Long dash followed by two dots
MSO_LINE_DASH_STYLE.SYSTEM_DASH # System dash style
MSO_LINE_DASH_STYLE.SYSTEM_DOT # System dot style
MSO_LINE_DASH_STYLE.SYSTEM_DASH_DOT # System dash-dot styleUsage examples:
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
from pptx.enum.dml import MSO_FILL_TYPE, MSO_LINE_DASH_STYLE, MSO_THEME_COLOR_INDEX
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[6])
# Create shape with solid fill
rect1 = slide.shapes.add_shape(
MSO_AUTO_SHAPE_TYPE.RECTANGLE,
Inches(1), Inches(1), Inches(2), Inches(1)
)
# Solid fill with RGB color
rect1.fill.solid()
rect1.fill.fore_color.rgb = RGBColor(255, 0, 0) # Red fill
# Border formatting
rect1.line.color.rgb = RGBColor(0, 0, 255) # Blue border
rect1.line.width = Pt(3)
rect1.line.dash_style = MSO_LINE_DASH_STYLE.DASH
# Create shape with gradient fill
rect2 = slide.shapes.add_shape(
MSO_AUTO_SHAPE_TYPE.RECTANGLE,
Inches(4), Inches(1), Inches(2), Inches(1)
)
# Gradient fill
rect2.fill.gradient()
rect2.fill.gradient_angle = 45 # 45-degree gradient
rect2.fill.fore_color.rgb = RGBColor(0, 255, 0) # Green
rect2.fill.back_color.rgb = RGBColor(0, 0, 255) # Blue
# Create shape with theme colors
rect3 = slide.shapes.add_shape(
MSO_AUTO_SHAPE_TYPE.OVAL,
Inches(1), Inches(3), Inches(2), Inches(1)
)
# Theme color fill
rect3.fill.solid()
rect3.fill.fore_color.theme_color = MSO_THEME_COLOR_INDEX.ACCENT_1
rect3.fill.fore_color.brightness = 0.5 # Lighter version
# Pattern fill example
rect4 = slide.shapes.add_shape(
MSO_AUTO_SHAPE_TYPE.RECTANGLE,
Inches(4), Inches(3), Inches(2), Inches(1)
)
rect4.fill.patterned()
rect4.fill.pattern = MSO_PATTERN_TYPE.LARGE_CHECKER_BOARD
rect4.fill.fore_color.rgb = RGBColor(255, 255, 0) # Yellow
rect4.fill.back_color.rgb = RGBColor(128, 0, 128) # Purple
# Text formatting with colors
textbox = slide.shapes.add_textbox(Inches(1), Inches(5), Inches(5), Inches(1))
tf = textbox.text_frame
tf.text = "Colorful text example"
# Format text color
p = tf.paragraphs[0]
p.font.color.rgb = RGBColor(255, 255, 255) # White text
# Text background (shape fill)
textbox.fill.solid()
textbox.fill.fore_color.rgb = RGBColor(0, 100, 0) # Dark green background
# Shadow effect
textbox.shadow.visible = True
textbox.shadow.color.rgb = RGBColor(128, 128, 128) # Gray shadow
textbox.shadow.blur_radius = Pt(4)
textbox.shadow.distance = Pt(3)
textbox.shadow.angle = 45
# Working with transparency
transparent_shape = slide.shapes.add_shape(
MSO_AUTO_SHAPE_TYPE.RECTANGLE,
Inches(2), Inches(2), Inches(1), Inches(1)
)
transparent_shape.fill.solid()
transparent_shape.fill.fore_color.rgb = RGBColor(255, 0, 255) # Magenta
# Note: Transparency would be set through additional properties if available
prs.save('formatting-example.pptx')# Create color from hex string
red = RGBColor.from_string('FF0000')
blue = RGBColor.from_string('#0000FF')
# Color manipulation
def lighten_color(color: RGBColor, factor: float = 0.3) -> RGBColor:
"""Lighten an RGB color by mixing with white."""
r = min(255, int(color.r + (255 - color.r) * factor))
g = min(255, int(color.g + (255 - color.g) * factor))
b = min(255, int(color.b + (255 - color.b) * factor))
return RGBColor(r, g, b)
def darken_color(color: RGBColor, factor: float = 0.3) -> RGBColor:
"""Darken an RGB color."""
r = max(0, int(color.r * (1 - factor)))
g = max(0, int(color.g * (1 - factor)))
b = max(0, int(color.b * (1 - factor)))
return RGBColor(r, g, b)
# Apply consistent color scheme
primary_color = RGBColor(31, 78, 120)
secondary_color = lighten_color(primary_color, 0.4)
accent_color = RGBColor(255, 192, 0)
# Corporate color palette
corporate_colors = {
'primary': RGBColor(0, 102, 204), # Blue
'secondary': RGBColor(102, 102, 102), # Gray
'accent': RGBColor(255, 153, 0), # Orange
'success': RGBColor(0, 153, 76), # Green
'warning': RGBColor(255, 193, 7), # Yellow
'danger': RGBColor(220, 53, 69), # Red
}
# Apply corporate branding
def apply_corporate_style(shape, style_type='primary'):
"""Apply corporate color scheme to shape."""
color = corporate_colors.get(style_type, corporate_colors['primary'])
shape.fill.solid()
shape.fill.fore_color.rgb = color
# Complementary border
if style_type == 'primary':
shape.line.color.rgb = corporate_colors['secondary']
else:
shape.line.color.rgb = corporate_colors['primary']
shape.line.width = Pt(1)def create_multi_stop_gradient(shape, colors_and_positions):
"""Create gradient with multiple color stops."""
shape.fill.gradient()
# Note: Multi-stop gradients would require accessing gradient stops
# collection if available in the implementation
# Basic two-color gradient
if len(colors_and_positions) >= 2:
shape.fill.fore_color.rgb = colors_and_positions[0][0]
shape.fill.back_color.rgb = colors_and_positions[-1][0]
# Example usage
gradient_colors = [
(RGBColor(255, 0, 0), 0.0), # Red at start
(RGBColor(255, 255, 0), 0.5), # Yellow at middle
(RGBColor(0, 255, 0), 1.0), # Green at end
]
shape = slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.RECTANGLE,
Inches(1), Inches(1), Inches(3), Inches(2))
create_multi_stop_gradient(shape, gradient_colors)