Skip to content

Instantly share code, notes, and snippets.

@MarWeUMR
Created July 23, 2024 19:06
Show Gist options
  • Save MarWeUMR/5e579bd47fa070f303e7bc434735e9a1 to your computer and use it in GitHub Desktop.
Save MarWeUMR/5e579bd47fa070f303e7bc434735e9a1 to your computer and use it in GitHub Desktop.
Cloudera
"""
Used to create a simple powerpoint table from some json input.
To get the json use e.g.:
curl --header 'Authorization: Basic $CREDS_64' 'https://<BASE_URL>/gateway/cdp-proxy-api/nifi-app/nifi-api/controller-services/d74f7a69-4b38-3a09-9a59-a2894e543856' | jq '.com
ponent.properties'
"""
import json
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor
def run(inData, title):
try:
data = json.loads(inData)
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return
# Create a new presentation
prs = Presentation()
# Add a new slide
slide_layout = prs.slide_layouts[5] # Use a blank slide layout
slide = prs.slides.add_slide(slide_layout)
# Define table dimensions
rows = len(data) + 2 # +2 for the title row and header row
cols = 2
# Add a table to the slide
left = Inches(0.5)
top = Inches(1)
width = Inches(9)
table = slide.shapes.add_table(rows, cols, left, top, width, Inches(0.1)).table
# Apply a built-in table style
table.style = "Medium Style 2 - Accent 1"
# Set column widths
table.columns[0].width = Inches(4)
table.columns[1].width = Inches(5)
# Merge the first row for the title
cell = table.cell(0, 0)
other_cell = table.cell(0, 1)
cell.merge(other_cell)
# Set the title in the merged cell
title_cell = table.cell(0, 0)
title_cell.text = title
title_cell.vertical_anchor = MSO_ANCHOR.MIDDLE
title_cell.text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER
title_cell.text_frame.paragraphs[0].font.bold = True
title_cell.text_frame.paragraphs[0].font.size = Pt(16)
title_cell.text_frame.word_wrap = True
title_cell.text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
# Add headers
table.cell(1, 0).text = "Properties"
table.cell(1, 1).text = "Values"
# Populate the table with data
for i, (key, value) in enumerate(data.items(), start=2):
table.cell(i, 0).text = key
table.cell(i, 1).text = str(value) if value is not None else ""
# Apply styling to all cells
for row_idx, row in enumerate(table.rows):
if row_idx == 0:
row.height = Inches(0.4) # Slightly taller for title
elif row_idx == 1:
row.height = Inches(0.3) # Slightly taller for header
else:
row.height = Inches(0.25) # Slightly taller for data rows
for col_idx, cell in enumerate(row.cells):
cell.vertical_anchor = MSO_ANCHOR.MIDDLE
cell.margin_top = Inches(0.01)
cell.margin_bottom = Inches(0.01)
for paragraph in cell.text_frame.paragraphs:
paragraph.space_before = Pt(0)
paragraph.space_after = Pt(0)
if cell.text: # Only set font size if there's text in the cell
if row_idx == 0: # Title row
paragraph.font.size = Pt(16)
paragraph.alignment = PP_ALIGN.CENTER
elif row_idx == 1: # Header row
paragraph.font.size = Pt(14)
paragraph.alignment = PP_ALIGN.CENTER
else: # Data rows
paragraph.font.size = Pt(11)
if col_idx == 0: # First column (Properties)
paragraph.alignment = PP_ALIGN.LEFT
else: # Second column (Values)
paragraph.alignment = PP_ALIGN.CENTER
paragraph.font.name = "Calibri"
# Save the presentation
try:
prs.save("/mnt/c/Users/Marcus/Desktop/json_data_table.pptx")
print("PowerPoint presentation created successfully!")
except Exception as e:
print(f"Error saving PowerPoint presentation: {e}")
def main() -> int:
json_data = """{
"Database Connection URL": "jdbc:hive2://<FQDN_ZOOKEEPER_1>:2181,<FQDN_ZOOKEEPER_2>:2181,<FQDN_ZOOKEEPER_3>:2181/default;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2",
"Hive Configuration Resources": "/etc/hive/conf/core-site.xml,/etc/hive/conf/hive-site.xml,/etc/hadoop/conf/hdfs-site.xml",
"Kerberos Principal": "<Ihr Kerberos Principal>",
"Kerberos Password": "********"
}"""
title = "Hive3ConnectionPool"
run(json_data, title)
return 0
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment