-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
99 lines (85 loc) · 3.18 KB
/
utils.py
File metadata and controls
99 lines (85 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Common utils useful across most of 500k automation tools
import os
import zipfile
from shutil import copyfile
from pptx.dml.color import RGBColor
import comtypes.client
import logging
def copy_unzip_docx(f_path):
# Copy docx and change file extension to *.zip
f_path_zip = f_path.split(".")[0] + ".zip"
copyfile(f_path, f_path_zip)
# unzip docx (now zip) file
dir_path = f_path.rsplit("\\", 1)[0]
zip_ref = zipfile.ZipFile(f_path_zip, 'r')
zip_ref.extractall(f_path.rsplit(".", 1)[0] + "_zip")
zip_ref.close()
return dir_path
def find_pic_in_docx(directory):
# Finds first pic from docx and returns path to image.
for dirName, subdirList, fileList in os.walk(directory):
for fname in fileList:
if fname.lower().endswith(('.png', '.jpg', '.jpeg')):
return dirName + "\\" + fname
# Formatting for bio lines
def bio_line(category, text, placeholder):
run = placeholder.add_run()
run.text = category
run.font.bold = True
run.font.color.rgb = RGBColor(89, 89, 89)
run = placeholder.add_run()
run.text = text
run.font.color.rgb = RGBColor(89, 89, 89)
def PPTtoPDF(inputFileName, outputFileName, formatType=32):
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
if outputFileName[-3:] != 'pdf':
outputFileName = outputFileName + ".pdf"
try:
deck = powerpoint.Presentations.Open(inputFileName)
try:
deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
except Exception as e:
logging.exception("Failed to save powerpoint to PDF")
deck.Close()
except Exception as e:
logging.exception("Failed to open powerpoint for conversion")
powerpoint.Quit()
def build_slide(prs):
# Call the standard template for the report slides
slide_layout = prs.slide_layouts[SLD_LAYOUT_TITLE_AND_CONTENT]
slide = prs.slides.add_slide(slide_layout)
def validate_state(state, abbreviation=False, convert_to_full=False):
state_dict = {"AN": "Andaman Islands",
"AP": "Andhra Pradesh",
"AS": "Assam",
"CH": "Chhattisgarh",
"HR": "Haryana",
"GJ": "Gujarat",
"HP": "Himachal Pradesh",
"JK": "Jammu and Kashmir",
"KA": "Karnataka",
"KL": "Kerala",
"MP": "Madhya Pradesh",
"MH": "Maharashtra",
"OR": "Odisha (Orissa)",
"PB": "Punjab",
"RJ": "Rajasthan",
"TN": "Tamil Nadu",
"TA": "Telangana",
"TR": "Tripura",
"UP": "Uttar Pradesh",
"UK": "Uttarakhand"}
if abbreviation:
if state in state_dict.keys():
if convert_to_full:
return state_dict[state]
else:
return state
else:
raise ValueError("Invalid state {0}".format(state))
else:
if state[0] in state_dict:
return state
else:
raise ValueError("Invalid state {0}".format(state))