Post by joedf » 18 Nov 2019, 12:03
Voici l'exemple.
Il faudra que tu ajustes/adaptes les variables
7z_exe,
DocumentODT et la fonction
ModifierLeContenu().
Code: Select all
#NoEnv
SetWorkingDir %A_ScriptDir%
; spécifier l'exécutable 7z
7z_exe:="7za.exe"
DocumentODT := "Test2.odt"
if FileExist(DocumentODT) {
; Creer un dossier temporaire
tmpDir := A_Temp "\tempODT"
if FileExist(tmpDir)
FileRemoveDir, %tmpDir%, 1
FileCreateDir, %tmpDir%
fContentXML := "content.xml"
tmpFile := tmpDir "\" fContentXML
; Extraire les fichiers
7z_extract(DocumentODT, tmpDir)
; lire le contenu de content.xml
FileRead, fileContents, %tmpFile%
; modifier le contenu
fileContents := ModifierLeContenu(fileContents)
; remplacer l'original
FileDelete, %tmpFile%
FileAppend, %fileContents%, %tmpFile%
; recombiner et recompresser le fichier
SplitPath, DocumentODT,,,, fNameNoExt
newFile := fNameNoExt ".nouveau.odt"
if FileExist(newFile)
FileDelete, %newFile%
7z_compressZIP(newFile, tmpDir "\*")
; Fin
MsgBox, 64, Fini!, Un nouveay fichier "%newFile%" a été créé.
} else {
MsgBox, 16, Erreur, Le fichier "%DocumentODT%" n'existe pas...
}
return
ModifierLeContenu(data) {
FRS2 := "42"
ADRESSE := "42 rue Allouette"
CP := "AZERTY"
REJET := "Non, jamais :("
StringReplace, data, data, FOURNISSEUR, %FRS2%
StringReplace, data, data, ADRESSE, %ADRESSE%
StringReplace, data, data, POSTAL, %CP%
StringReplace, data, data, NUMERO, %REJET%
return data
}
;modification de 7z wrapper - par joedf
; https://autohotkey.com/boards/viewtopic.php?f=6&t=3892
7z_compressZIP(dPack,files*) {
global 7z_exe
flist:=""
for each, file in files
flist:= flist """" file """" " "
if FileExist(dPack)
FileDelete, %dPack%
RunWait, %7z_exe% a -tZip "%dPack%" %flist%,,Hide UseErrorLevel
return !7z_error(ErrorLevel)
}
7z_extract(dPack,dFolder="",opts="") {
global 7z_exe
if StrLen(dFolder)
out:="-o" . """" . dFolder . """"
RunWait, %7z_exe% x "%dPack%" %out% -y %opts%,,Hide UseErrorLevel
return !7z_error(ErrorLevel)
}
7z_error(e) {
if (e==1)
MsgBox, 48, 7zip Error, Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed.
else if (e==2)
MsgBox, 48, 7zip Error, Fatal error
else if (e==7)
MsgBox, 48, 7zip Error, Command line error
else if (e==8)
MsgBox, 48, 7zip Error, Not enough memory for operation
else if (e==255)
MsgBox, 48, 7zip Error, User stopped the process
return e
}
Voici l'exemple. :thumbup:
Il faudra que tu ajustes/adaptes les variables [c]7z_exe[/c], [c]DocumentODT[/c] et la fonction [c]ModifierLeContenu()[/c].
[Codebox=autohotkey file=modiferODT.ahk]#NoEnv
SetWorkingDir %A_ScriptDir%
; spécifier l'exécutable 7z
7z_exe:="7za.exe"
DocumentODT := "Test2.odt"
if FileExist(DocumentODT) {
; Creer un dossier temporaire
tmpDir := A_Temp "\tempODT"
if FileExist(tmpDir)
FileRemoveDir, %tmpDir%, 1
FileCreateDir, %tmpDir%
fContentXML := "content.xml"
tmpFile := tmpDir "\" fContentXML
; Extraire les fichiers
7z_extract(DocumentODT, tmpDir)
; lire le contenu de content.xml
FileRead, fileContents, %tmpFile%
; modifier le contenu
fileContents := ModifierLeContenu(fileContents)
; remplacer l'original
FileDelete, %tmpFile%
FileAppend, %fileContents%, %tmpFile%
; recombiner et recompresser le fichier
SplitPath, DocumentODT,,,, fNameNoExt
newFile := fNameNoExt ".nouveau.odt"
if FileExist(newFile)
FileDelete, %newFile%
7z_compressZIP(newFile, tmpDir "\*")
; Fin
MsgBox, 64, Fini!, Un nouveay fichier "%newFile%" a été créé.
} else {
MsgBox, 16, Erreur, Le fichier "%DocumentODT%" n'existe pas...
}
return
ModifierLeContenu(data) {
FRS2 := "42"
ADRESSE := "42 rue Allouette"
CP := "AZERTY"
REJET := "Non, jamais :("
StringReplace, data, data, FOURNISSEUR, %FRS2%
StringReplace, data, data, ADRESSE, %ADRESSE%
StringReplace, data, data, POSTAL, %CP%
StringReplace, data, data, NUMERO, %REJET%
return data
}
;modification de 7z wrapper - par joedf
; https://autohotkey.com/boards/viewtopic.php?f=6&t=3892
7z_compressZIP(dPack,files*) {
global 7z_exe
flist:=""
for each, file in files
flist:= flist """" file """" " "
if FileExist(dPack)
FileDelete, %dPack%
RunWait, %7z_exe% a -tZip "%dPack%" %flist%,,Hide UseErrorLevel
return !7z_error(ErrorLevel)
}
7z_extract(dPack,dFolder="",opts="") {
global 7z_exe
if StrLen(dFolder)
out:="-o" . """" . dFolder . """"
RunWait, %7z_exe% x "%dPack%" %out% -y %opts%,,Hide UseErrorLevel
return !7z_error(ErrorLevel)
}
7z_error(e) {
if (e==1)
MsgBox, 48, 7zip Error, Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed.
else if (e==2)
MsgBox, 48, 7zip Error, Fatal error
else if (e==7)
MsgBox, 48, 7zip Error, Command line error
else if (e==8)
MsgBox, 48, 7zip Error, Not enough memory for operation
else if (e==255)
MsgBox, 48, 7zip Error, User stopped the process
return e
}
[/Codebox]