Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- .gitattributes +2 -0
- NRL3OHS019-IP-Work-Instruction-Briefing-v1.pptx +3 -0
- README.md +3 -8
- flagged/log.csv +2 -0
- modified_presentation.pptx +3 -0
- ppt_automation.ipynb +325 -0
- ppt_automation.py +68 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
NRL3OHS019-IP-Work-Instruction-Briefing-v1.pptx filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
modified_presentation.pptx filter=lfs diff=lfs merge=lfs -text
|
NRL3OHS019-IP-Work-Instruction-Briefing-v1.pptx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ec6e6f2fd19ec362c91afe75b299898b80cf03a02c65cf9144cf6ee29aa8d0e4
|
| 3 |
+
size 1812693
|
README.md
CHANGED
|
@@ -1,12 +1,7 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.44.4
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Powerpoint_Automation
|
| 3 |
+
app_file: ppt_automation.ipynb
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
sdk_version: 3.44.4
|
|
|
|
|
|
|
| 6 |
---
|
| 7 |
+
# ppt_automation
|
|
|
flagged/log.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
keyword,output,flag,username,timestamp
|
| 2 |
+
hazard,Keywordhazrdnot found in the presentation.,,,2023-09-20 17:32:19.518856
|
modified_presentation.pptx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:32fe856f2301e62246108310de5c207e3487b55b25ff70d7cc28ae69a62d354e
|
| 3 |
+
size 1791876
|
ppt_automation.ipynb
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "2da53e7f",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"from pptx import Presentation\n",
|
| 11 |
+
"from pptx.dml.color import RGBColor\n",
|
| 12 |
+
"from pptx.util import Pt, Inches\n",
|
| 13 |
+
"from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE\n",
|
| 14 |
+
"from pptx import Presentation\n",
|
| 15 |
+
"from pptx.util import Pt\n",
|
| 16 |
+
"from pptx.enum.text import PP_ALIGN\n",
|
| 17 |
+
"\n",
|
| 18 |
+
"# Import useful library \n",
|
| 19 |
+
"import pandas as pd\n",
|
| 20 |
+
"import warnings\n",
|
| 21 |
+
"import os"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 2,
|
| 27 |
+
"id": "e16bdb19",
|
| 28 |
+
"metadata": {},
|
| 29 |
+
"outputs": [],
|
| 30 |
+
"source": [
|
| 31 |
+
"prs = Presentation('NRL3OHS019-IP-Work-Instruction-Briefing-v1.pptx')"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"execution_count": 3,
|
| 37 |
+
"id": "b7d9d8a6",
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"outputs": [],
|
| 40 |
+
"source": [
|
| 41 |
+
"# for slide in list(prs.slides):\n",
|
| 42 |
+
"# print((slide.slide_id))"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": 4,
|
| 48 |
+
"id": "e8006002",
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"outputs": [],
|
| 51 |
+
"source": [
|
| 52 |
+
"def search_keywords_in_ppt(prs, keyword):\n",
|
| 53 |
+
"\n",
|
| 54 |
+
" textbox_number = 0\n",
|
| 55 |
+
"\n",
|
| 56 |
+
" for slide_number, slide in enumerate(prs.slides, start=1):\n",
|
| 57 |
+
" for shape_number, shape in enumerate(slide.shapes, start=1):\n",
|
| 58 |
+
" if shape.has_text_frame:\n",
|
| 59 |
+
" text = shape.text\n",
|
| 60 |
+
" if keyword in text:\n",
|
| 61 |
+
" print(f\"Keyword '{keyword}' found in Slide {slide_number}, Textbox {shape_number}\")\n",
|
| 62 |
+
" textbox_number += 1\n",
|
| 63 |
+
"\n",
|
| 64 |
+
" if textbox_number == 0:\n",
|
| 65 |
+
" print(f\"Keyword '{keyword}' not found in the presentation.\")\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"def search_and_replace_keywords_in_ppt(prs, keyword, new_text):\n",
|
| 70 |
+
" found_keywords = False\n",
|
| 71 |
+
"\n",
|
| 72 |
+
" for slide in prs.slides:\n",
|
| 73 |
+
" for shape in slide.shapes:\n",
|
| 74 |
+
" if shape.has_text_frame:\n",
|
| 75 |
+
" text_frame = shape.text_frame\n",
|
| 76 |
+
" for paragraph in text_frame.paragraphs:\n",
|
| 77 |
+
" for run in paragraph.runs:\n",
|
| 78 |
+
" if keyword in run.text:\n",
|
| 79 |
+
" # Clear the entire text frame\n",
|
| 80 |
+
" for p in text_frame.paragraphs:\n",
|
| 81 |
+
" p.clear()\n",
|
| 82 |
+
" # Add the new text to the text frame\n",
|
| 83 |
+
" p = text_frame.paragraphs[0]\n",
|
| 84 |
+
" run = p.add_run()\n",
|
| 85 |
+
" run.text = str(new_text)\n",
|
| 86 |
+
" run.font.size = Pt(18) # Set the font size as needed\n",
|
| 87 |
+
" p.alignment = PP_ALIGN.LEFT # Set alignment as needed\n",
|
| 88 |
+
"\n",
|
| 89 |
+
" found_keywords = True\n",
|
| 90 |
+
"\n",
|
| 91 |
+
" if found_keywords:\n",
|
| 92 |
+
" prs.save(\"modified_presentation.pptx\")\n",
|
| 93 |
+
" print(f\"Keywords replaced and presentation saved as 'modified_presentation.pptx'.\")\n",
|
| 94 |
+
" else:\n",
|
| 95 |
+
" print(f\"Keywords '{keyword}' not found in the presentation.\")\n"
|
| 96 |
+
]
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"cell_type": "code",
|
| 100 |
+
"execution_count": 5,
|
| 101 |
+
"id": "c746d1b6",
|
| 102 |
+
"metadata": {},
|
| 103 |
+
"outputs": [
|
| 104 |
+
{
|
| 105 |
+
"name": "stdout",
|
| 106 |
+
"output_type": "stream",
|
| 107 |
+
"text": [
|
| 108 |
+
"Keyword 'Hazard' found in Slide 5, Textbox 1\n",
|
| 109 |
+
"Keyword 'Hazard' found in Slide 7, Textbox 1\n",
|
| 110 |
+
"Keyword 'Hazard' found in Slide 7, Textbox 3\n"
|
| 111 |
+
]
|
| 112 |
+
}
|
| 113 |
+
],
|
| 114 |
+
"source": [
|
| 115 |
+
"search_keywords_in_ppt(prs, \"Hazard\")"
|
| 116 |
+
]
|
| 117 |
+
},
|
| 118 |
+
{
|
| 119 |
+
"cell_type": "code",
|
| 120 |
+
"execution_count": 6,
|
| 121 |
+
"id": "794a0519",
|
| 122 |
+
"metadata": {},
|
| 123 |
+
"outputs": [
|
| 124 |
+
{
|
| 125 |
+
"name": "stdout",
|
| 126 |
+
"output_type": "stream",
|
| 127 |
+
"text": [
|
| 128 |
+
"Keywords replaced and presentation saved as 'modified_presentation.pptx'.\n"
|
| 129 |
+
]
|
| 130 |
+
}
|
| 131 |
+
],
|
| 132 |
+
"source": [
|
| 133 |
+
"prs = Presentation('NRL3OHS019-IP-Work-Instruction-Briefing-v1.pptx')\n",
|
| 134 |
+
"keyword_to_search = \"Hazard =\"\n",
|
| 135 |
+
"new_text = f\"\"\"In the context of Network Rail, a company responsible for managing and maintaining the United Kingdom's rail infrastructure, the distinction between hazard and risk plays a critical role in ensuring the safety and reliability of the rail network. Hazards refer to potential sources of harm or danger, such as faulty tracks, electrical issues, or adverse weather conditions. Network Rail must identify and assess these hazards to understand the threats they pose to the railway system. On the other hand, risk involves the likelihood and consequences of these hazards leading to accidents or disruptions. Network Rail's risk management efforts aim to mitigate and control these risks through proactive measures like regular inspections, maintenance, and safety protocols.\"\"\"\n",
|
| 136 |
+
"search_and_replace_keywords_in_ppt(prs, keyword_to_search, new_text)"
|
| 137 |
+
]
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"cell_type": "code",
|
| 141 |
+
"execution_count": 51,
|
| 142 |
+
"id": "ace7c061",
|
| 143 |
+
"metadata": {},
|
| 144 |
+
"outputs": [
|
| 145 |
+
{
|
| 146 |
+
"name": "stdout",
|
| 147 |
+
"output_type": "stream",
|
| 148 |
+
"text": [
|
| 149 |
+
"Running on local URL: http://127.0.0.1:7891\n",
|
| 150 |
+
"Running on public URL: https://080dd8d2f48f784f62.gradio.live\n",
|
| 151 |
+
"\n",
|
| 152 |
+
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
|
| 153 |
+
]
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"data": {
|
| 157 |
+
"text/html": [
|
| 158 |
+
"<div><iframe src=\"https://080dd8d2f48f784f62.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 159 |
+
],
|
| 160 |
+
"text/plain": [
|
| 161 |
+
"<IPython.core.display.HTML object>"
|
| 162 |
+
]
|
| 163 |
+
},
|
| 164 |
+
"metadata": {},
|
| 165 |
+
"output_type": "display_data"
|
| 166 |
+
},
|
| 167 |
+
{
|
| 168 |
+
"name": "stderr",
|
| 169 |
+
"output_type": "stream",
|
| 170 |
+
"text": [
|
| 171 |
+
"Traceback (most recent call last):\n",
|
| 172 |
+
" File \"/opt/homebrew/lib/python3.11/site-packages/gradio/routes.py\", line 516, in predict\n",
|
| 173 |
+
" output = await route_utils.call_process_api(\n",
|
| 174 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 175 |
+
" File \"/opt/homebrew/lib/python3.11/site-packages/gradio/route_utils.py\", line 219, in call_process_api\n",
|
| 176 |
+
" output = await app.get_blocks().process_api(\n",
|
| 177 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 178 |
+
" File \"/opt/homebrew/lib/python3.11/site-packages/gradio/blocks.py\", line 1440, in process_api\n",
|
| 179 |
+
" data = self.postprocess_data(fn_index, result[\"prediction\"], state)\n",
|
| 180 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 181 |
+
" File \"/opt/homebrew/lib/python3.11/site-packages/gradio/blocks.py\", line 1303, in postprocess_data\n",
|
| 182 |
+
" self.validate_outputs(fn_index, predictions) # type: ignore\n",
|
| 183 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
| 184 |
+
" File \"/opt/homebrew/lib/python3.11/site-packages/gradio/blocks.py\", line 1278, in validate_outputs\n",
|
| 185 |
+
" raise ValueError(\n",
|
| 186 |
+
"ValueError: An event handler (search_keyword) didn't receive enough output values (needed: 4, received: 2).\n",
|
| 187 |
+
"Wanted outputs:\n",
|
| 188 |
+
" [textbox, state, textbox, button]\n",
|
| 189 |
+
"Received outputs:\n",
|
| 190 |
+
" [\"Keyword as not found in the presentation.\", False]\n"
|
| 191 |
+
]
|
| 192 |
+
}
|
| 193 |
+
],
|
| 194 |
+
"source": [
|
| 195 |
+
"import gradio as gr\n",
|
| 196 |
+
"\n",
|
| 197 |
+
"theme = gr.themes.Base(\n",
|
| 198 |
+
" primary_hue=\"indigo\",\n",
|
| 199 |
+
" secondary_hue=\"blue\",\n",
|
| 200 |
+
" neutral_hue=\"slate\",\n",
|
| 201 |
+
" font=[gr.themes.GoogleFont('open sans'), 'ui-sans-serif', 'system-ui', 'sans-serif'],\n",
|
| 202 |
+
").set(body_text_color='*secondary_950', \n",
|
| 203 |
+
" button_primary_background_fill=\"*secondary_950\",\n",
|
| 204 |
+
" button_primary_background_fill_dark=\"*secondary_950\",\n",
|
| 205 |
+
" button_primary_background_fill_hover=\"*primary_950\",\n",
|
| 206 |
+
" button_primary_background_fill_hover_dark=\"*primary_950\",\n",
|
| 207 |
+
" )\n",
|
| 208 |
+
"\n",
|
| 209 |
+
" \n",
|
| 210 |
+
"css = \"\"\"\n",
|
| 211 |
+
"#warning {background: red;} \n",
|
| 212 |
+
"\n",
|
| 213 |
+
".btn { background-color: #0A0F58CC !important; color: #FFFFFF !important}\n",
|
| 214 |
+
".btn:hover { background-color: #0A0F58 !important; color: #FFFFFF !important}\n",
|
| 215 |
+
"\"\"\" \n",
|
| 216 |
+
" \n",
|
| 217 |
+
"\n",
|
| 218 |
+
"with gr.Blocks(theme=theme, css=css) as demo:\n",
|
| 219 |
+
" gr.Markdown(\n",
|
| 220 |
+
" \"\"\"\n",
|
| 221 |
+
" <div style=\"display: flex; flex-direction: column; align-items: flex-start; justify-content: flex-start;\">\n",
|
| 222 |
+
" <div style=\"display: flex; align-items: center; justify-content: center;\">\n",
|
| 223 |
+
" <img src=\"https://images.nationalrail.co.uk/e8xgegruud3g/4SEen29dfdF6mdj7e9afzA/06fa8f3b1fb3e610c652f50d10809fed/National_Rail_Icon.png\" alt=\"National Rail Logo\" width=\"50\" height=\"50\" />\n",
|
| 224 |
+
" <h1 class=\"text\" style=\"margin: 0; margin-left: 10px; font-size: 50px;\">National Rail</h1>\n",
|
| 225 |
+
" </div>\n",
|
| 226 |
+
" <h2 class=\"text\" >Powerpoint Batch Editor!</h2> \n",
|
| 227 |
+
" <p class=\"text\" style=\"margin-top:10px;\">Start Typing Below to Find Keyword Occurences.</p>\n",
|
| 228 |
+
" </div> \n",
|
| 229 |
+
" \"\"\")\n",
|
| 230 |
+
" hasKeyword = gr.State(value=False)\n",
|
| 231 |
+
" inp = gr.Textbox(placeholder=\"Enter Keyword\", label=\"Keyword\")\n",
|
| 232 |
+
" btn = gr.Button(value=\"Search\", elem_classes=\"btn\")\n",
|
| 233 |
+
" out = gr.Textbox(label=\"Occurences\")\n",
|
| 234 |
+
" \n",
|
| 235 |
+
" inp2 = gr.Textbox(placeholder=\"Enter Replacement Paragraph\", label=\"Paragraph Replace\", visible=True)\n",
|
| 236 |
+
" btn2 = gr.Button(value=\"Replace\", visible=False, elem_classes=\"btn\")\n",
|
| 237 |
+
" out2 = gr.Textbox(label=\"Result\", visible=False)\n",
|
| 238 |
+
" \n",
|
| 239 |
+
" def search_keyword(keyword):\n",
|
| 240 |
+
" textbox_number = 0\n",
|
| 241 |
+
" response = []\n",
|
| 242 |
+
" \n",
|
| 243 |
+
" for slide_number, slide in enumerate(prs.slides, start=1):\n",
|
| 244 |
+
" for shape_number, shape in enumerate(slide.shapes, start=1):\n",
|
| 245 |
+
" if shape.has_text_frame:\n",
|
| 246 |
+
" text = shape.text\n",
|
| 247 |
+
" if keyword in text:\n",
|
| 248 |
+
" textbox_number += 1\n",
|
| 249 |
+
" response.append(f'Keyword \"{keyword}\" found in Slide {slide_number}, Textbox {shape_number}')\n",
|
| 250 |
+
"\n",
|
| 251 |
+
" if textbox_number > 0:\n",
|
| 252 |
+
" return [\"\\n\".join(response), True, gr.update(visible=True), gr.update(visible=True)]\n",
|
| 253 |
+
" if textbox_number == 0:\n",
|
| 254 |
+
" return [\"Keyword \" + keyword + \" not found in the presentation.\", False]\n",
|
| 255 |
+
" \n",
|
| 256 |
+
" btn.click(fn=search_keyword, inputs=inp, outputs=[out, hasKeyword, out2, btn2])\n",
|
| 257 |
+
" \n",
|
| 258 |
+
" fileOut = gr.File(visible=False)\n",
|
| 259 |
+
" \n",
|
| 260 |
+
" def replace_and_return_modified(keyword, new_text):\n",
|
| 261 |
+
" found_keywords = False\n",
|
| 262 |
+
"\n",
|
| 263 |
+
" for slide in prs.slides:\n",
|
| 264 |
+
" for shape in slide.shapes:\n",
|
| 265 |
+
" if shape.has_text_frame:\n",
|
| 266 |
+
" text_frame = shape.text_frame\n",
|
| 267 |
+
" for paragraph in text_frame.paragraphs:\n",
|
| 268 |
+
" for run in paragraph.runs:\n",
|
| 269 |
+
" if keyword in run.text:\n",
|
| 270 |
+
" # Clear the entire text frame\n",
|
| 271 |
+
" for p in text_frame.paragraphs:\n",
|
| 272 |
+
" p.clear()\n",
|
| 273 |
+
" # Add the new text to the text frame\n",
|
| 274 |
+
" p = text_frame.paragraphs[0]\n",
|
| 275 |
+
" run = p.add_run()\n",
|
| 276 |
+
" run.text = str(new_text)\n",
|
| 277 |
+
" run.font.size = Pt(18) # Set the font size as needed\n",
|
| 278 |
+
" p.alignment = PP_ALIGN.LEFT # Set alignment as needed\n",
|
| 279 |
+
"\n",
|
| 280 |
+
" found_keywords = True\n",
|
| 281 |
+
"\n",
|
| 282 |
+
" if found_keywords:\n",
|
| 283 |
+
" prs.save(\"modified_presentation.pptx\")\n",
|
| 284 |
+
" return [f\"Keywords replaced and presentation saved as 'modified_presentation.pptx'.\", gr.update(value=\"modified_presentation.pptx\",visible=True)]\n",
|
| 285 |
+
" else:\n",
|
| 286 |
+
" return f\"Keywords '{keyword}' not found in the presentation.\"\n",
|
| 287 |
+
" \n",
|
| 288 |
+
" btn2.click(fn=replace_and_return_modified, inputs=[inp, inp2], outputs=[out2, fileOut])\n",
|
| 289 |
+
" \n",
|
| 290 |
+
"\n",
|
| 291 |
+
"if __name__ == \"__main__\":\n",
|
| 292 |
+
" demo.launch(share=True) \n"
|
| 293 |
+
]
|
| 294 |
+
},
|
| 295 |
+
{
|
| 296 |
+
"cell_type": "code",
|
| 297 |
+
"execution_count": null,
|
| 298 |
+
"id": "1baede91",
|
| 299 |
+
"metadata": {},
|
| 300 |
+
"outputs": [],
|
| 301 |
+
"source": []
|
| 302 |
+
}
|
| 303 |
+
],
|
| 304 |
+
"metadata": {
|
| 305 |
+
"kernelspec": {
|
| 306 |
+
"display_name": "Python 3 (ipykernel)",
|
| 307 |
+
"language": "python",
|
| 308 |
+
"name": "python3"
|
| 309 |
+
},
|
| 310 |
+
"language_info": {
|
| 311 |
+
"codemirror_mode": {
|
| 312 |
+
"name": "ipython",
|
| 313 |
+
"version": 3
|
| 314 |
+
},
|
| 315 |
+
"file_extension": ".py",
|
| 316 |
+
"mimetype": "text/x-python",
|
| 317 |
+
"name": "python",
|
| 318 |
+
"nbconvert_exporter": "python",
|
| 319 |
+
"pygments_lexer": "ipython3",
|
| 320 |
+
"version": "3.11.5"
|
| 321 |
+
}
|
| 322 |
+
},
|
| 323 |
+
"nbformat": 4,
|
| 324 |
+
"nbformat_minor": 5
|
| 325 |
+
}
|
ppt_automation.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pptx import Presentation
|
| 2 |
+
from pptx.dml.color import RGBColor
|
| 3 |
+
from pptx.util import Pt, Inches
|
| 4 |
+
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
|
| 5 |
+
from pptx import Presentation
|
| 6 |
+
from pptx.util import Pt
|
| 7 |
+
from pptx.enum.text import PP_ALIGN
|
| 8 |
+
|
| 9 |
+
# Import useful library
|
| 10 |
+
import pandas as pd
|
| 11 |
+
import warnings
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
prs = Presentation('NRL3OHS019-IP-Work-Instruction-Briefing-v1.pptx')
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def search_keywords_in_ppt(prs, keyword):
|
| 18 |
+
textbox_number = 0
|
| 19 |
+
|
| 20 |
+
for slide_number, slide in enumerate(prs.slides, start=1):
|
| 21 |
+
for shape_number, shape in enumerate(slide.shapes, start=1):
|
| 22 |
+
if shape.has_text_frame:
|
| 23 |
+
text = shape.text
|
| 24 |
+
if keyword in text:
|
| 25 |
+
print(
|
| 26 |
+
"Keyword '{keyword}' found in Slide {slide_number}, Textbox {shape_number}")
|
| 27 |
+
textbox_number += 1
|
| 28 |
+
|
| 29 |
+
if textbox_number == 0:
|
| 30 |
+
print("Keyword '{keyword}' not found in the presentation.")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def search_and_replace_keywords_in_ppt(prs, keyword, new_text):
|
| 34 |
+
found_keywords = False
|
| 35 |
+
|
| 36 |
+
for slide in prs.slides:
|
| 37 |
+
for shape in slide.shapes:
|
| 38 |
+
if shape.has_text_frame:
|
| 39 |
+
text_frame = shape.text_frame
|
| 40 |
+
for paragraph in text_frame.paragraphs:
|
| 41 |
+
for run in paragraph.runs:
|
| 42 |
+
if keyword in run.text:
|
| 43 |
+
# Clear the entire text frame
|
| 44 |
+
for p in text_frame.paragraphs:
|
| 45 |
+
p.clear()
|
| 46 |
+
# Add the new text to the text frame
|
| 47 |
+
p = text_frame.paragraphs[0]
|
| 48 |
+
run = p.add_run()
|
| 49 |
+
run.text = str(new_text)
|
| 50 |
+
# Set the font size as needed
|
| 51 |
+
run.font.size = Pt(18)
|
| 52 |
+
p.alignment = PP_ALIGN.LEFT # Set alignment as needed
|
| 53 |
+
|
| 54 |
+
found_keywords = True
|
| 55 |
+
|
| 56 |
+
if found_keywords:
|
| 57 |
+
prs.save("modified_presentation.pptx")
|
| 58 |
+
print("Keywords replaced and presentation saved as 'modified_presentation.pptx'.")
|
| 59 |
+
else:
|
| 60 |
+
print("Keywords '{keyword}' not found in the presentation.")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
search_keywords_in_ppt(prs, "Hazard")
|
| 64 |
+
|
| 65 |
+
prs = Presentation('NRL3OHS019-IP-Work-Instruction-Briefing-v1.pptx')
|
| 66 |
+
keyword_to_search = "Hazard ="
|
| 67 |
+
new_text = """In the context of Network Rail, a company responsible for managing and maintaining the United Kingdom's rail infrastructure, the distinction between hazard and risk plays a critical role in ensuring the safety and reliability of the rail network. Hazards refer to potential sources of harm or danger, such as faulty tracks, electrical issues, or adverse weather conditions. Network Rail must identify and assess these hazards to understand the threats they pose to the railway system. On the other hand, risk involves the likelihood and consequences of these hazards leading to accidents or disruptions. Network Rail's risk management efforts aim to mitigate and control these risks through proactive measures like regular inspections, maintenance, and safety protocols."""
|
| 68 |
+
search_and_replace_keywords_in_ppt(prs, keyword_to_search, new_text)
|