Python-Docx et la tableau - Comment mettre en place des styles particuler dans chaque cellule d'un tableau
bonjour
Je cherche a obtenir un document word avec un tableau .
Avec une alternance de cellule : cellule de titre centré de type "Dark List" ,suivi d'une cellule style "Normale"
avec du text formater pouvant prendre plusieurs lignes.
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
|
# 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, Cm
#from docx.enum.text import WD_ALIGN_PARAGRAPH
'''
Created on 4 janv. 2023
@author:
'''
def bloc_entete_de_page(document):
section_entete =document.sections[0].header
paragraph_entete = section_entete.paragraphs[0]
logo = paragraph_entete.add_run()
#logo.add_picture('image001.png' , width=Cm(4.72), height= Cm(1.56))
text_run = paragraph_entete.add_run()
text_run.text = "Formulaire AP0152"
text_run.font.size =Pt(16)
pass
def bloc_1(document):
info_general = "\n" + "Ce formulaire permet dindiquer les caractéristiques " + \
"\n" + "Pour toute question sur le remplissage de ce formulaire, merci de saisir une demande dinformation "
document.add_heading('', level=0)
table = document.add_table(rows=5, cols=1)
table.style="Dark List"
#able.alignment = WD_ALIGN_PARAGRAPH.CENTER
hdr_cells = table.rows[0].cells
hdr_cells = table.rows[0].cells[0].text = "information_ générales"
hdr_cells = table.rows[1].cells[0].text = info_general
hdr_cells = table.rows[2].cells[0].text = 'Nous contacter'
hdr_cells = table.rows[3].cells[0].text = ''
hdr_cells = table.rows[4].cells[0].text = 'Équipe : Alpha Omega' + '/n' + 'Adresse : Alpha_Omega@Montruc.fr'
if __name__ == '__main__':
document = Document()
bloc_entete_de_page(document)
bloc_1(document)
document.save("exemple2.docx") |
Je parviens a affecter le style a tous le tableau mais je ne parviens pas a modifier une cellule de façon indépendante des autres.
un solution pourrait être de créer 1 tableau pour chaque cellule.
J'aimerai par exemple que l'URL ressorte de façon traditionnelle en bleu.
Autre point, je me demande si ce module pour word python-docx peut créer des objets tel que des "cases a cocher "... j'en doute.car je n'ai rien trouvé dans la documentation qui soit susceptible de le faire.....
pas de maeilleur résultat je tourne en rond
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') |
j'ai résolue la 2ier partie.
j'ai résolue la 2ier partie.