Created
March 5, 2022 13:08
-
-
Save Mihonarium/d5f3d96d92dea7864944641c7c550b10 to your computer and use it in GitHub Desktop.
A find-and-replace in multiple .docx files, most of the code is generated by Copilot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/nguyenthenguyen/docx" | |
"net/http" | |
"os" | |
"strings" | |
) | |
var files []struct{ | |
d *docx.Docx | |
name string | |
} | |
func main() { | |
// make a subdirectory in the current directory for changed docs | |
os.Mkdir("changed", 0777) | |
// read the list of files in the current working directory | |
dirFiles, _ := os.ReadDir(".") | |
// iterate through all files in the current directory, open the files and add to the files var | |
for _, file := range dirFiles { | |
if !strings.HasSuffix(file.Name(), ".docx") { | |
continue | |
} | |
r, err := docx.ReadDocxFile(file.Name()) | |
if err != nil { | |
fmt.Println("Error on", file.Name(), err.Error(), "skipping") | |
continue | |
} | |
d := r.Editable() | |
files = append(files, struct{ | |
d *docx.Docx | |
name string | |
}{d, file.Name()}) | |
} | |
// make an api the web interface will call | |
http.HandleFunc("/api", func(writer http.ResponseWriter, request *http.Request) { | |
// get the string to replace | |
replace := request.FormValue("replace") | |
// get the string to replace with | |
with := request.FormValue("with") | |
// iterate through all files and replace the string | |
for _, file := range files { | |
file.d.Replace(replace, with, -1) | |
} | |
// save all the files | |
for _, file := range files { | |
file.d.WriteToFile("changed/" + file.name) | |
} | |
// return a success message | |
writer.Write([]byte("Success!")) | |
}) | |
// display the interface for replacing strings in all the files | |
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { | |
html := `<html> | |
<head> | |
<title>Replace String</title> | |
<!-- add jquery --> | |
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script> | |
</head> | |
<body> | |
<input type="text" id="replace" name="replace" placeholder="Replace"> | |
<input type="text" name="with" id="with" placeholder="With"> | |
<input type="submit" value="Replace" id="submit"> | |
<div id="results"></div> | |
<script> | |
var submit = document.getElementById("submit"); | |
submit.addEventListener("click", function(e) { | |
var replace = document.getElementById("replace").value; | |
var w = document.getElementById("with").value; | |
var results = document.getElementById("results"); | |
$.ajax({ | |
url: "/api", | |
type: "POST", | |
data: { | |
replace: replace, | |
with: w | |
}, | |
success: function(data) { | |
results.innerHTML += "<p>" + replace + " -> " + w + "</p>"; | |
} | |
}); | |
}); | |
</script> | |
</body>` | |
writer.Write([]byte(html)) | |
}) | |
http.ListenAndServe(":8080", nil) | |
waiter := make(chan bool) | |
<-waiter | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment