-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
170 lines (153 loc) · 6.38 KB
/
Copy pathscript.py
File metadata and controls
170 lines (153 loc) · 6.38 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
from docx import Document
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
def connexion(driver):
url_login = ""
driver.get(url_login)
username = ""
password = ""
try:
champ_username = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "login"))
)
champ_password = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "password"))
)
bouton_connexion = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "bg-dendreo-primary"))
)
champ_username.clear()
champ_password.clear()
champ_username.send_keys(username)
champ_password.send_keys(password)
bouton_connexion.click()
except Exception as e:
print(f"Erreur de connexion : {e}")
def lister_fichiers(dossier):
fichiers = []
for fichier in os.listdir(dossier):
if os.path.isfile(os.path.join(dossier, fichier)):
nom_sans_underscore = fichier.replace("_", " ")
nom_sans_extension = nom_sans_underscore.rsplit(".", 1)[0]
fichiers.append(nom_sans_extension)
return fichiers
def extraire_titres_existants(driver):
titres = set() # Utiliser un ensemble pour éviter les doublons
catalogue = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "catalogue"))
)
catalogue.click()
while True:
# Attendre que tous les éléments soient présents
WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.CLASS_NAME, "dd-mini-link--module"))
)
time.sleep(1) # Ajouter une courte pause pour être sûr que tous les éléments se chargent
# Collecter les titres uniques
elements = driver.find_elements(By.CLASS_NAME, "dd-mini-link--module")
for element in elements:
titre = element.get_attribute("title")
if titre: # Ajouter le titre seulement s'il n'est pas vide
titres.add(titre)
# Vérifier si le bouton "Suivant" est activé
try:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1) # Pause pour permettre au bouton de devenir cliquable
bouton_suivant = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "dataTableBuilder_next"))
)
# Vérifier si le bouton est désactivé, puis arrêter la boucle
classe_bouton = bouton_suivant.get_attribute("class")
if classe_bouton and "disabled" in classe_bouton:
print("Fin de la pagination. Le bouton 'Suivant' est désactivé.")
break
else:
bouton_suivant.click()
WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.CLASS_NAME, "dd-mini-link--module"))
)
except Exception as e:
print("Erreur lors de la pagination :", e)
break
titres_liste = list(titres) # Convertir l'ensemble en liste pour le retour
print("Titres existants trouvés :", titres_liste)
return titres_liste
def lire_contenu_docx(chemin_fichier):
document = Document(chemin_fichier)
contenu = []
for para in document.paragraphs:
contenu.append(para.text)
return '\n'.join(contenu)
def ajout_intitule(driver, fichier):
driver.get("")
intitule = driver.find_element(By.ID, "intitule")
intitule.clear()
intitule.send_keys(fichier)
def ajout_description(driver, fichier, dossier):
chemin_fichier = os.path.join(dossier, fichier) + ".docx"
chemin_fichier = chemin_fichier.replace(" ", "_")
contenu = lire_contenu_docx(chemin_fichier)
driver.switch_to.frame(driver.find_element(By.ID, "description_ifr"))
champ_contenu = driver.find_element(By.TAG_NAME, "body")
champ_contenu.clear()
champ_contenu.send_keys(contenu)
driver.switch_to.default_content()
def valider(driver):
time.sleep(2)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2)
Valider = driver.find_element(By.ID, "sub_add_module")
Valider.click()
def mettre_en_forme(driver):
bouton_assistant_ai = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, "//div[@id='p_description']//button[@data-mce-name='dd_ai_assistant']"))
)
bouton_assistant_ai.click()
option_mettre_en_forme = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, "//div[contains(text(), 'Mettre en forme')]"))
)
option_mettre_en_forme.click()
bouton_remplacer = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Remplacer']"))
)
bouton_remplacer.click()
def activer_ia(driver, div_id):
bouton_assistant_ai = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, f"//div[@id='{div_id}']//button[@data-mce-name='dd_ai_assistant']"))
)
bouton_assistant_ai.click()
option_generer_contenu = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, "//div[contains(text(), 'Générer du contenu')]"))
)
option_generer_contenu.click()
bouton_remplacer = WebDriverWait(driver, 60).until(
EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Remplacer']"))
)
bouton_remplacer.click()
time.sleep(2)
if __name__ == "__main__":
dossier = "./Files/"
driver = webdriver.Firefox()
try:
connexion(driver)
fichiers_a_traiter = lister_fichiers(dossier)
titres_existants = extraire_titres_existants(driver)
fichiers_a_ajouter = [f for f in fichiers_a_traiter if f not in titres_existants]
for fichier in fichiers_a_ajouter:
ajout_intitule(driver, fichier)
ajout_description(driver, fichier, dossier)
mettre_en_forme(driver)
time.sleep(3)
activer_ia(driver, "p_")
activer_ia(driver, "p_")
activer_ia(driver, "p_")
activer_ia(driver, "p_")
activer_ia(driver, "p_")
# input("continuer?")
valider(driver)
finally:
driver.quit()