Creating Presentations with Python: OpenOffice Impress Compatibility
I'm creating a pptx file with Python. However, depending on the app you use to view the file, the results will be different. In PowerPoint: it works correctly. In OO Impress: othwerwise, the text is white. I would like to find a way to generate black text in any pptx file viewing app.
Steps to riproduce the bug;
1. Compile the source code
2. Open ot with OpenOffice Impress
import collections.abc
from pptx import Presentation
from pptx.util import Inches, Pt, Cm
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.text import MSO_ANCHOR, PP_ALIGN
from pptx.dml.color import RGBColor
from pptx.oxml.ns import qn
from lxml import etree
def html(hex_colore):
hex_colore = hex_colore.lstrip("#")
r = int(hex_colore[0:2], 16)
g = int(hex_colore[2:4], 16)
b = int(hex_colore[4:6], 16)
return RGBColor(r, g, b)
def applica_stile_rilievo(shape, colore_hex, intensita=3):
"""Applica lo stile 'Effetto intenso' (rilievo + ombra + sfumatura)
usando il colore indicato, sfruttando le ricette del tema di default.
intensita: 1 = sottile, 2 = moderato, 3 = intenso (quello dello screenshot)."""
sp = shape._element
style = sp.find(qn('p:style'))
if style is None:
style = etree.SubElement(sp, qn('p:style'))
def imposta_colore(tag, idx):
ref = style.find(qn(f'a:{tag}'))
if ref is None:
ref = etree.SubElement(style, qn(f'a:{tag}'))
ref.set('idx', str(idx))
for child in list(ref):
ref.remove(child)
c = etree.SubElement(ref, qn('a:srgbClr'))
c.set('val', colore_hex)
imposta_colore('lnRef', 2)
imposta_colore('fillRef', intensita)
imposta_colore('effectRef', intensita)
# rimuove riempimento e bordo espliciti: altrimenti "coprono"
# la sfumatura/il bordo che arrivano dallo stile del tema
spPr = sp.spPr
for tag in ('a:solidFill', 'a:ln'):
el = spPr.find(qn(tag))
if el is not None:
spPr.remove(el)
# Crea una nuova presentazione
prs = Presentation()
prs.slide_width = Cm(37.34)
prs.slide_height = Cm(21)
# --- Slide 1: titolo ---
layout_titolo = prs.slide_layouts[2]
slide = prs.slides.add_slide(layout_titolo)
sfondo = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE, 0, 0,
prs.slide_width, prs.slide_height
)
sfondo.fill.solid()
sfondo.fill.fore_color.rgb = html("#383E42")
sfondo.line.fill.background() # Rimuove il bordo del rettangolo
# riquadro: x,y,w,h
riquadro = slide.shapes.add_shape(
MSO_SHAPE.ROUNDED_RECTANGLE,
Cm((37.34/2)-6), # sinistra
Cm(1), # alto
Cm(12), # larghezza
Cm(2), # altezza
)
#angolo spigoli
riquadro.adjustments[0] = 0.15
#sfondo
applica_stile_rilievo(riquadro, colore_hex="FFFF00", intensita=4)
#testo
# Assegna il testo (crea un run predefinito)
riquadro.text = "ISO/IEC 17025"
testo = riquadro.text_frame
testo.vertical_anchor = MSO_ANCHOR.MIDDLE
paragrafo = testo.paragraphs[0]
paragrafo.alignment = PP_ALIGN.CENTER
# Applica lo stile direttamente ai singoli RUN (compatibile con Impress e PowerPoint)
for run in paragrafo.runs:
run.font.name = "Calibri"
run.font.size = Pt(36)
run.font.color.rgb = RGBColor(0, 0, 0) # oppure html("#000000") se restituisce RGBColor
# --- Salvataggio finale ---
prs.save("ISO_IEC 17025.pptx")
print("Presentazione creata: mia_presentazione.pptx")
