GenProp / src /tools /doc_tools.py
adrien.aribaut-gaudin
feat: new public GenProp
498db6b
from PIL import Image
import os
import docx
import docx.oxml.ns as ns
def get_positions(xml_file):
i = 0
width = xml_file.split('cx="')
height = xml_file.split('cy="')
while(i < len(width)):
temp = width[i].split('"')[0]
if(temp.isnumeric()):
width = temp
break
else:
i+=1
i = 0
while(i < len(height)):
temp = height[i].split('"')[0]
if(temp.isnumeric()):
height = temp
break
else:
i+=1
return width, height
def convert_to_png(imageslist):
for image in imageslist:
if(image.endswith('.png')):
continue
im = Image.open(image)
im.save(image.split('.')[0]+'.png')
imageslist[imageslist.index(image)] = image.split('.')[0]+'.png'
os.remove(image)
return imageslist
def get_difference_with_template(styles_used_in_doc, template):
styles_used_in_template = template.styles.names
different_styles = []
for style in styles_used_in_doc:
if style.name not in styles_used_in_template:
if style.name not in [s.name for s in different_styles]:
different_styles.append(style)
return different_styles
def update_table_of_contents(doc):
# Find the settings element in the document
settings_element = doc.settings.element
# Create an "updateFields" element and set its "val" attribute to "true"
update_fields_element = docx.oxml.shared.OxmlElement('w:updateFields')
update_fields_element.set(ns.qn('w:val'), 'true')
# Add the "updateFields" element to the settings element
settings_element.append(update_fields_element)
def left_part_until_number(s):
for i, char in enumerate(s):
if char.isdigit():
return s[:i]
return None
def get_title(path) -> str:
if '/' not in path and '\\' not in path:
res = path
if '/' in path:
res = path.split('/')[-1]
if '\\' in path:
res = path.split('\\')[-1]
return res