Cela ne changer rien au résultat
Version imprimable
je suis repartie du xml du word .
j'ai trouver le paragraphe qui m'interesse
Code:
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 <w:p w14:paraId="2956ED24" w14:textId="7F1B7600" w:rsidR="000842EA" w:rsidRDefault="000842EA" w:rsidP="005E6032"> <w:pPr> <w:pStyle w:val="Texteduchamp"/> </w:pPr> <w:r> <w:t xml:space="preserve"equipe : </w:t> </w:r> <w:r> <w:fldChar w:fldCharType="begin"> <w:ffData> <w:name w:val="Text7"/> <w:enabled/> <w:calcOnExit w:val="0"/> <w:statusText w:type="text" w:val="Saisir votre texte ici"/> <w:textInput/> </w:ffData> </w:fldChar> </w:r> <w:r> <w:instrText xml:space="preserve"> FORMTEXT </w:instrText> </w:r> <w:r> <w:fldChar w:fldCharType="separate"/> </w:r> <w:r w:rsidR="00E30A18"> <w:t> </w:t> </w:r> <w:r w:rsidR="00E30A18"> <w:t> </w:t> </w:r> <w:r w:rsidR="00E30A18"> <w:t> </w:t> </w:r> <w:r w:rsidR="00E30A18"> <w:t> </w:t> </w:r> <w:r w:rsidR="00E30A18"> <w:t> </w:t> </w:r> <w:r> <w:fldChar w:fldCharType="end"/> </w:r> </w:p>
j'ai chercher a constuire celui-Code:
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 # coding: utf-8 from docx import Document from docx.oxml import OxmlElement from docx.oxml.ns import qn from docx.shared import Pt def bloc_fomulaire(titre_du_champ, contenue_champ): w_p1 =OxmlElement('w:p') #w_p1.set(qn('w14:paraId'), "2956ED24") #w_p1.set(qn('w14:textId'), "7F1B7600") #w_p1.set(qn('w:rsidRDefault'), "000842EA") #w_p1.set(qn('w:rsidP'), "005E6032") #<w:pPr> # <w:pStyle w:val="Texteduchamp"/> #</w:pPr> w_pPr=OxmlElement('w:pPr') # bloc pPr w_pStyle = OxmlElement("w:pStyle") w_pStyle.set(qn('w:val'),"Texteduchamp") w_pPr.append(w_pStyle) w_p1.append(w_pPr) #<w:r> # <w:t xml:space="preserve">Demandeur HM*(équipe) : </w:t> #</w:r> w_r=OxmlElement('w:r') # bloc r w_t=OxmlElement('w:t') w_t.set(qn('xml:space'),"preserve") w_t.text= titre_du_champ w_r.append(w_t) w_p1.append(w_r) # <w:r> # <w:fldChar w:fldCharType="begin"> # <w:ffData> # <w:name w:val="Text7"/> # <w:enabled/> # <w:calcOnExit w:val="0"/> # <w:statusText w:type="text" # w:val="Saisir votre texte ici"/> # <w:textInput/> # </w:ffData> # </w:fldChar> # </w:r> w_r = OxmlElement('w:r') w_fldChar = OxmlElement('w:fldChar') w_fldChar.set(qn('w:fldCharType'), 'begin') w_ffData = OxmlElement('w:ffData') w_name = OxmlElement('w:name') w_name.set(qn('w:val'), 'Text7') w_enabled = OxmlElement('w:enabled') w_calcOnExit = OxmlElement('w:calcOnExit') w_calcOnExit.set(qn('w:val'), '0') w_statusText = OxmlElement('w:statusText') w_statusText.set(qn('w:type'), 'text') w_statusText.set(qn('w:val'), 'Saisir votre texte ici') w_textInput = OxmlElement('w:textInput') w_ffData.append(w_name) w_ffData.append(w_enabled) w_ffData.append(w_calcOnExit) w_ffData.append(w_statusText) w_ffData.append(w_textInput) w_fldChar.append(w_ffData) w_r.append(w_fldChar) w_p1.append(w_r) #<w:r> # <w:instrText xml:space="preserve"> FORMTEXT </w:instrText> #</w:r> w_r = OxmlElement('w:r') w_instrText = OxmlElement("w:instrText") w_instrText.set(qn("xml:space"), "preserve") w_instrText.text = " FORMTEXT " w_r.append(w_instrText) w_p1.append(w_r) #<w:r> # <w:instrText xml:space="preserve"> FORMTEXT </w:instrText> #</w:r> w_r = OxmlElement('w:r') w_fldChar = OxmlElement('w:fldChar') w_fldChar.set(qn('w:fldCharType'), 'separate') w_r.append(w_fldChar) w_r = OxmlElement('w:r') w_p1.append(w_r) #<w:r w:rsidR="00E30A18"> # <w:t> </w:t> #</w:r> w_r = OxmlElement('w:r') w_r.set(qn('w:rsidR'), "00E30A18") w_t = OxmlElement('w:t') w_t.text = contenue_champ w_r.append(w_t) w_p1.append(w_r) return w_p1 if __name__ == '__main__': document = Document() # On ajoute un nouveau paragraphe contenant un champ de formulaire en appelant la fonction bloc_formulaire p = document.add_paragraph() p._element.append(bloc_fomulaire("Titre du champ", "Contenu initial du champ")) # On sauvegarde le document document.save('mon_document.docx')
malheureusement lorsque l'on ouvre ce fichier sous word on a une erreur de saturation mémoire je me demande si cela n'est pas lier a ceci
voici ce qui apparait sur word a l'ouvertureCode:
1
2
3
4 w_p1.set(qn('w14:paraId'), "2956ED24") w_p1.set(qn('w14:textId'), "7F1B7600") w_p1.set(qn('w:rsidRDefault'), "000842EA") w_p1.set(qn('w:rsidP'), "005E6032")
Pièce jointe 634678
j'ai cherche a tronquer le code OXML
l'effet est similiaire...Code:
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 # coding: utf-8 from docx import Document from docx.oxml import OxmlElement from docx.oxml.ns import qn from docx.shared import Pt def bloc_fomulaire(titre_du_champ, contenue_champ): w_p1 =OxmlElement('w:p') w_pPr=OxmlElement('w:pPr') # bloc pPr w_pStyle = OxmlElement("w:pStyle") w_pStyle.set(qn('w:val'),"Texteduchamp") w_pPr.append(w_pStyle) w_p1.append(w_pPr) return w_p1 if __name__ == '__main__': document = Document() # On ajoute un nouveau paragraphe contenant un champ de formulaire en appelant la fonction bloc_formulaire p = document.add_paragraph() p._element.append(bloc_fomulaire("Titre du champ", "Contenu initial du champ")) # On sauvegarde le document document.save('mon_document.docx')
Code:
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 # coding: utf-8 from docx import Document from docx.oxml import OxmlElement from docx.oxml.ns import qn from docx.shared import Pt from docx.enum.text import WD_PARAGRAPH_ALIGNMENT from docx.enum.style import WD_STYLE_TYPE def bloc_formulaire(titre_du_champ, contenu_champ): p = OxmlElement("w:p") pPr = OxmlElement("w:pPr") pStyle = OxmlElement("w:pStyle") pStyle.set(qn("w:val"), "Texteduchamp") pPr.append(pStyle) p.append(pPr) r1 = OxmlElement("w:r") t1 = OxmlElement("w:t") t1.text = titre_du_champ + ": " r1.append(t1) p.append(r1) r2 = OxmlElement("w:r") fldChar1 = OxmlElement("w:fldChar") fldChar1.set(qn("w:fldCharType"), "begin") r2.append(fldChar1) p.append(r2) r3 = OxmlElement("w:r") instrText = OxmlElement("w:instrText") instrText.set(qn("xml:space"), "preserve") instrText.text = " FORMTEXT " r3.append(instrText) p.append(r3) r4 = OxmlElement("w:r") fldChar2 = OxmlElement("w:fldChar") fldChar2.set(qn("w:fldCharType"), "separate") r4.append(fldChar2) p.append(r4) r5 = OxmlElement("w:r") t2 = OxmlElement("w:t") t2.text = contenu_champ r5.append(t2) p.append(r5) r6 = OxmlElement("w:r") fldChar3 = OxmlElement("w:fldChar") fldChar3.set(qn("w:fldCharType"), "end") r6.append(fldChar3) p.append(r6) return p if __name__ == '__main__': document = Document() # Ajouter le style "Texteduchamp" new_style = document.styles.add_style('Texteduchamp', WD_STYLE_TYPE.PARAGRAPH) # Définir les propriétés du style new_style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER font = new_style.font font.name = 'Arial' font.size = Pt(12) font.bold = True # Ajouter un nouveau paragraphe contenant un champ de formulaire p = document.add_paragraph(style=new_style) p._element.append(bloc_formulaire("Titre du champ", "Contenu initial du champ")) # Sauvegarder le document document.save('mon_document.docx')
voici le code qui permet d'avoir le formulaire intégré sans erreur .
Pas toujours simple.
Ne pouvant pas partie du document d'origine a modifier j'ai créer une version simplifié avec cet unique champ après activation du mode développeur de word . l'utilisation de formulaire hérité pour créer une zone d'édition (contrôle de formulaire).
J'ai sauvegardé le fichier word en XML pour retrouver le code xml
qui donne ceci
et donc voici le code finaliséCode:
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 xml="""<w:p w14:paraId="451EF9BB" w14:textId="456635BC" w:rsidR="00F13FB1" w:rsidRPr="00EC6D63" w:rsidRDefault="002370D5" w:rsidP="00EC6D63"> <w:r> <w:t>Champ de saisie*:</w:t> </w:r> <w:r> <w:fldChar w:fldCharType="begin"> <w:ffData> <w:name w:val="Texte1"/> <w:enabled/> <w:calcOnExit w:val="0"/> <w:textInput/> </w:ffData> </w:fldChar> </w:r> <w:bookmarkStart w:id="0" w:name="Texte1"/> <w:r> <w:instrText xml:space="preserve"> FORMTEXT </w:instrText> </w:r> <w:r> <w:fldChar w:fldCharType="separate"/> </w:r> <w:r> <w:rPr> <w:noProof/> </w:rPr> <w:t> </w:t> </w:r> <w:r> <w:rPr> <w:noProof/> </w:rPr> <w:t> </w:t> </w:r> <w:r> <w:rPr> <w:noProof/> </w:rPr> <w:t> </w:t> </w:r> <w:r> <w:rPr> <w:noProof/> </w:rPr> <w:t> </w:t> </w:r> <w:r> <w:rPr> <w:noProof/> </w:rPr> <w:t> </w:t> </w:r> <w:r> <w:fldChar w:fldCharType="end"/> </w:r> <w:bookmarkEnd w:id="0"/> </w:p> """
celui-ci reste toutefois limitéCode:
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 # coding: utf-8 from docx import Document from docx.shared import Pt from docx.oxml import OxmlElement from docx.oxml.ns import qn def create_text_input_paragraph(name): p = OxmlElement('w:p') pPr = OxmlElement('w:pPr') p.append(pPr) # Create a run containing the label for the text input field label_run = create_run('Champ de saisie :') p.append(label_run) # Create a run containing the text input field input_run = create_text_input_run(name) p.append(input_run) return p def create_run(text): r = OxmlElement('w:r') t = OxmlElement('w:t') t.text = text r.append(t) return r def create_text_input_run(name): r = OxmlElement('w:r') # Create the field character for the text input field fldChar_start = OxmlElement('w:fldChar') fldChar_start.set(qn('w:fldCharType'), 'begin') r.append(fldChar_start) # Create the form field data ffData = OxmlElement('w:ffData') name_element = OxmlElement('w:name') name_element.set(qn('w:val'), name) ffData.append(name_element) ffData.append(OxmlElement('w:enabled')) ffData.append(OxmlElement('w:calcOnExit')) ffData.append(OxmlElement('w:textInput')) r.append(ffData) # Create the instruction text for the field instrText = OxmlElement('w:instrText') instrText.set(qn('xml:space'), 'preserve') instrText.text = ' FORMTEXT ' r.append(instrText) # Create the field separator character fldChar_sep = OxmlElement('w:fldChar') fldChar_sep.set(qn('w:fldCharType'), 'separate') r.append(fldChar_sep) # Create the field result t = OxmlElement('w:r') tPr = OxmlElement('w:rPr') tPr.append(OxmlElement('w:noProof')) t.append(tPr) t.text = ' ********* ' r.append(t) # Create the field end character fldChar_end = OxmlElement('w:fldChar') fldChar_end.set(qn('w:fldCharType'), 'end') r.append(fldChar_end) return r # Créer un document document = Document() # Ajouter un paragraphe contenant le champ de formulaire p = document.add_paragraph() p.add_run().element.append(create_text_input_paragraph('texte1')) # Enregistrer le document document.save('test.docx')
j'ai cherche a variabilité
Code:
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 # coding: utf-8 from docx import Document from docx.shared import Pt from docx.oxml import OxmlElement from docx.oxml.ns import qn def create_text_input_paragraph(name,titre_du_champ, valeur): p = OxmlElement('w:p') pPr = OxmlElement('w:pPr') p.append(pPr) # Create a run containing the label for the text input field label_run = create_run(titre_du_champ) p.append(label_run) # Create a run containing the text input field input_run = create_text_input_run(name,valeur) p.append(input_run) return p def create_run(text): r = OxmlElement('w:r') t = OxmlElement('w:t') t.text = text r.append(t) return r def create_text_input_run(name,valeur): r = OxmlElement('w:r') # Create the field character for the text input field fldChar_start = OxmlElement('w:fldChar') fldChar_start.set(qn('w:fldCharType'), 'begin') r.append(fldChar_start) # Create the form field data ffData = OxmlElement('w:ffData') name_element = OxmlElement('w:name') name_element.set(qn('w:val'), name) ffData.append(name_element) ffData.append(OxmlElement('w:enabled')) ffData.append(OxmlElement('w:calcOnExit')) ffData.append(OxmlElement('w:textInput')) r.append(ffData) # Create the instruction text for the field instrText = OxmlElement('w:instrText') instrText.set(qn('xml:space'), 'preserve') instrText.text = ' FORMTEXT ' r.append(instrText) # Create the field separator character fldChar_sep = OxmlElement('w:fldChar') fldChar_sep.set(qn('w:fldCharType'), 'separate') r.append(fldChar_sep) # Create the field result t = OxmlElement('w:r') tPr = OxmlElement('w:rPr') tPr.append(OxmlElement('w:noProof')) t.append(tPr) t.text = valeur r.append(t) # Create the field end character fldChar_end = OxmlElement('w:fldChar') fldChar_end.set(qn('w:fldCharType'), 'end') r.append(fldChar_end) return r # Creer un document document = Document() # Ajouter un paragraphe contenant le champ de formulaire p = document.add_paragraph() p.add_run().element.append(create_text_input_paragraph('texte1',"champ",'la valeur du champ')) # Enregistrer le document document.save('test.docx')
cela marche mais uniquement avec 1 seule ligne de type
#
p.add_run().element.append(create_text_input_paragraph('texte1',"champ",'la valeur du champ'))
#
j'ai résolue la 2ier partie.
voici un autre cas avec les liens le dossier vient https://stackoverflow.com ›
Adding an hyperlink in MSWord by using python-docx
j'ai donc modifier l'original qui n'avez aucun import....Code:
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 # coding: utf-8 from docx import Document from docx.shared import Inches from docx.shared import RGBColor from docx.enum.text import WD_COLOR_INDEX from docx.shared import Inches ,Pt, RGBColor from docx.oxml.shared import OxmlElement from docx.oxml import OxmlElement from docx.oxml.ns import qn from docx.enum.dml import MSO_THEME_COLOR_INDEX def add_hyperlink(paragraph, url, text, color, underline): """ A function that places a hyperlink within a paragraph object. :param paragraph: The paragraph we are adding the hyperlink to. :param url: A string containing the required url :param text: The text displayed for the url :return: The hyperlink object """ # This gets access to the document.xml.rels file and gets a new relation id value part = paragraph.part r_id = part.relate_to(url, document.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True) # Create the w:hyperlink tag and add needed values hyperlink = OxmlElement.shared.OxmlElement('w:hyperlink') hyperlink.set(OxmlElement.shared.qn('r:id'), r_id, ) # Create a w:r element new_run = OxmlElement.shared.OxmlElement('w:r') # Create a new w:rPr element rPr = OxmlElement.shared.OxmlElement('w:rPr') # Add color if it is given if not color is None: c = OxmlElement.shared.OxmlElement('w:color') c.set(OxmlElement.shared.qn('w:val'), color) rPr.append(c) # Remove underlining if it is requested if not underline: u = OxmlElement.shared.OxmlElement('w:u') u.set(OxmlElement.shared.qn('w:val'), 'none') rPr.append(u) # Join all the xml elements together add add the required text to the w:r element new_run.append(rPr) new_run.text = text hyperlink.append(new_run) paragraph._p.append(hyperlink) return hyperlink document = Document() p = document.add_paragraph() #add a hyperlink with the normal formatting (blue underline) hyperlink = add_hyperlink(p, 'http://www.google.com', 'Google', None, True) #add a hyperlink with a custom color and no underline hyperlink = add_hyperlink(p, 'http://www.google.com', 'Google', 'FF8822', False) document.save('demo.docx')
mais voila dans la 1ier tentative on obtient une erreur
r_id = part.relate_to(url, document.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True)
AttributeError: 'Document' object has no attribute 'opc'
finalement la version originel fonctionne . seul effet de bord Eclips n'apprécie pas l'import générique . et laisse apparaitre des messages d'alerte sur tous les appels de fonction utilisant le package....
Code:
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 # coding: utf-8 import docx def add_hyperlink(paragraph, url, text, color, underline): """ A function that places a hyperlink within a paragraph object. :param paragraph: The paragraph we are adding the hyperlink to. :param url: A string containing the required url :param text: The text displayed for the url :return: The hyperlink object """ # This gets access to the document.xml.rels file and gets a new relation id value part = paragraph.part r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True) # Create the w:hyperlink tag and add needed values hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink') hyperlink.set(docx.oxml.shared.qn('r:id'), r_id, ) # Create a w:r element new_run = docx.oxml.shared.OxmlElement('w:r') # Create a new w:rPr element rPr = docx.oxml.shared.OxmlElement('w:rPr') # Add color if it is given if not color is None: c = docx.oxml.shared.OxmlElement('w:color') c.set(docx.oxml.shared.qn('w:val'), color) rPr.append(c) # Remove underlining if it is requested if not underline: u = docx.oxml.shared.OxmlElement('w:u') u.set(docx.oxml.shared.qn('w:val'), 'none') rPr.append(u) # Join all the xml elements together add add the required text to the w:r element new_run.append(rPr) new_run.text = text hyperlink.append(new_run) paragraph._p.append(hyperlink) return hyperlink document = docx.Document() p = document.add_paragraph() #add a hyperlink with the normal formatting (blue underline) hyperlink = add_hyperlink(p, 'http://www.google.com', 'Google', None, True) #add a hyperlink with a custom color and no underline hyperlink = add_hyperlink(p, 'http://www.google.com', 'Google', 'FF8822', False) document.save('demo.docx')
Complètement hors sujet mais ce ne serait pas plus simple de travailler en LaTeX plutôt que Word ?
Je comprends.