File size: 2,071 Bytes
498db6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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