File size: 1,391 Bytes
4cf88e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import docx
from docx.enum.style import WD_STYLE_TYPE
import os
from config import config
from typing import Dict
import random
import datetime
import string

from lxml import etree

from src.domain.doc import Doc




name = 'CorpTemplate.docx'

template_path = config['templates_path'] + '/' + config['templates'][config['default_template_index']]
template = Doc(template_path)
doc_path = config['these_docs_path'] + name
this_doc = Doc(path=doc_path)
new_doc_path = config['new_docs_path'] + this_doc.name + '_.docx'
new_doc = this_doc.copy(new_doc_path)




new_styles = new_doc.styles.xstyles
print(etree.tostring(new_styles['.Titre1'].element))
names = new_doc.styles.names
print(names)
new_doc.save_as_docx()


s = template.styles.xstyles['.BodyText']
# new_styles.add_style(s.name, WD_STYLE_TYPE.PARAGRAPH)


list_styles = [(s, s.name) for s in template.styles.xstyles if s.type==WD_STYLE_TYPE.LIST]


base_styles_set = set()
for s in new_styles:
    if s.type == 1:
        if s.base_style:
            try:
                base_styles_set.add(s.base_style.name)
            except:
                print(f"failure for {s}")


base_styles = list(base_styles_set)




"""
or p in new_doc.xdoc.paragraphs:
    if p.style == new_styles['_newBody__2']:
        p.style = s.name

new_styles['_newBody__2'].delete()
new_doc.save_as_docx()
"""
pass
etree.tostring(list_styles[1][0].element)