Path: buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 27 Jun 2022 09:06:00 -0500 Newsgroups: fr.comp.lang.python Subject: Remplir des champs identique dans plusieurs fichiers X-poster: PEAR::Net_NNTP v1.5.0 (stable) From: chahd1 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit Organization: !No_Organization! Message-ID: <3MadnXU7PIPVJCT_nZ2dnUU7_83NnZ2d@giganews.com> Date: Mon, 27 Jun 2022 09:06:00 -0500 Lines: 69 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-ynZgqmXOfjc6GPFXlVtoC9PFHHAsiz1O9VGLfaF9hKg5p0U7bXjXZadXVy/ThVNtdl0ktTIfDG2+Bic!OISMSHqIgnj3afCDYcQPlbM79M4FrkSImP7xZkdT9cFcEU2HErH1qiwPwH/SV+ywn+yNMK8aJNxi X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 Bytes: 3615 X-Original-Lines: 64 Bonjour, J'ai un programme qui modifie automatiquement des champs déjà définis dans plusieurs template .text . Par exemple, dans mon modèle, j'ai défini un champ defclients{#.......#} le code demande avec quoi je veux le remplacer . Parfois les substitutions ( les champs ) sont communes entre les différents fichiers, il est donc inutile qu'il me demande de les remplir plusieurs fois. comment puis-je résoudre ce problème ? merci d'avance . import os, subprocess, re def find_occurrences(template_tex): # Open template.tex with open(template_tex, "e;e;r"e;e;) as f: filedata = f.read() # Find occurrences of {{item}} return set(re.findall("e;e;#(.*?)#"e;e;, filedata)) def prompt_replacements(occurrences): replacements = dict() # Replacing fields for occurrence in occurrences: replaceby = input("e;e;Replace "e;e; + occurrence + "e;e; by: "e;e; ) replacements[occurrence] = replaceby return replacements def clean_latex_temporary_files(result_tex): filename_wext = result_tex[:result_tex.rfind("e;e;."e;e;)] for ext in ["e;e;tex"e;e;, "e;e;aux"e;e;, "e;e;log"e;e;]: print("e;e;rm"e;e;, filename_wext + "e;e;."e;e; + ext) os.remove(filename_wext + "e;e;."e;e; + ext) def compile_latex(result_tex): # Convert result.tex to result.pdf proc = subprocess.Popen(["e;e;pdflatex"e;e;, "e;e;-interaction=nonstopmode"e;e;, result_tex]) proc.communicate() clean_temporary_files(result_tex) def make_replaced_latex(template_tex, result_tex, replacements): # Write results with open(template_tex, "e;e;r"e;e;) as f: filedata = f.read() for (occurrence, replaceby) in replacements.items(): filedata = filedata.replace("e;e;{{"e;e; + occurrence + "e;e;}}"e;e;, replaceby) with open(result_tex, "e;e;w"e;e;) as f: f.write(filedata) def main(): map_template_result = { "e;e;template1.tex"e;e; : "e;e;result1.tex"e;e;, "e;e;template2.tex"e;e; : "e;e;result2.tex"e;e;, "e;e;template3.tex"e;e; : "e;e;result3.tex"e;e;, } occurrences = set() for template_tex in map_template_result.keys(): occurrences |= find_occurrences(template_tex) replacements = prompt_replacements(occurrences) for (template_tex, result_tex) in map_template_result.items(): make_replaced_latex(template_tex, result_tex, replacements) compile_latex(result_tex) clean_temporary_files(result_tex) main()