Python-pptxでヒートマップを作る

Python-pptxでパワーポイントにヒートマップを作る。ベースとして表を使う。

from datetime import datetime
import colorsys

from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor

import numpy as np

file_name = "{}.pptx".format(datetime.now().strftime("%Y-%m-%d-%H%M%S"))

presentation = Presentation("./template.pptx")
layout = presentation.slide_layouts[0]
for shape in layout.placeholders:
	print('%d %s' % (shape.placeholder_format.idx, shape.name))

slide = presentation.slides.add_slide(layout)
slide.placeholders[0].text = "Dash Board"

heatmap_data = np.random.rand(5, 20).flatten()
# PlaceholderにTableを挿入
table = slide.placeholders[13].insert_table(rows = 5, cols = 20).table

for i, cell in enumerate(table.iter_cells()):
		cell.text_frame.paragraphs[0].font.size = Pt(5)
		cell.fill.solid()
		cellCol = colorsys.hsv_to_rgb(0.7, heatmap_data[i], 1.0)
		cell.fill.fore_color.rgb = RGBColor(*[int(x*255) for x in cellCol])
		cell.height = Pt(5)

for row in table.rows:
		row.height = Inches(0.25)

for col in table.columns:
		col.width = Inches(0.25)

presentation.save(file_name)

No comments:

Post a Comment