docfusion/golang/main.go

126 lines
3.1 KiB
Go
Raw Normal View History

2024-07-05 13:13:57 +00:00
package main
import (
"fmt"
"io/ioutil"
"log"
2024-07-05 21:43:24 +00:00
"os"
2024-07-05 13:13:57 +00:00
"path/filepath"
2024-07-05 21:43:24 +00:00
"strings"
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
"gioui.org/app"
2024-07-18 07:55:34 +00:00
"gioui.org/font/gofont"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
2024-07-05 21:43:24 +00:00
"gioui.org/widget"
2024-07-18 07:55:34 +00:00
"gioui.org/widget/material"
2024-07-05 13:13:57 +00:00
2024-07-18 07:55:34 +00:00
"github.com/unidoc/unioffice/common/license"
2024-07-05 13:13:57 +00:00
"github.com/unidoc/unioffice/document"
)
2024-07-05 21:43:24 +00:00
func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
func main() {
go func() {
w := new(app.Window)
2024-07-18 07:55:34 +00:00
w.Option(app.Title("Fusionner DOCX"), app.Size(unit.Dp(800), unit.Dp(600)))
2024-07-05 21:43:24 +00:00
if err := loop(w); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
func loop(w *app.Window) error {
2024-07-18 07:55:34 +00:00
var ops op.Ops
2024-07-05 13:13:57 +00:00
2024-07-18 07:55:34 +00:00
th := material.NewTheme()
th.Shaper = text.NewShaper(text.WithCollection(gofont.Collection()))
2024-07-05 21:43:24 +00:00
var input widget.Editor
input.SingleLine = true
2024-07-18 07:55:34 +00:00
var button widget.Clickable
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
for {
2024-07-18 07:55:34 +00:00
evt := w.Event()
switch e := evt.(type) {
2024-07-05 21:43:24 +00:00
case app.DestroyEvent:
return e.Err
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
layout.Flex{
2024-07-18 07:55:34 +00:00
Axis: layout.Vertical,
2024-07-05 21:43:24 +00:00
Alignment: layout.Middle,
}.Layout(gtx,
layout.Rigid(material.H1(th, "Sélectionner un dossier").Layout),
layout.Rigid(material.Editor(th, &input, "Chemin du dossier").Layout),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
btn := material.Button(th, &button, "Fusionner")
btn.TextSize = unit.Sp(20)
for button.Clicked(gtx) {
dossier := strings.TrimSpace(input.Text())
if dossier != "" {
fusionnerDocx(dossier)
} else {
fmt.Println("Le chemin du dossier est vide")
2024-07-05 13:13:57 +00:00
}
2024-07-05 21:43:24 +00:00
}
return btn.Layout(gtx)
}),
)
e.Frame(&ops)
}
}
}
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
func fusionnerDocx(dossier string) {
docFusionne := document.New()
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
fichiers, err := ioutil.ReadDir(dossier)
if err != nil {
log.Fatalf("Erreur lors de la lecture du dossier : %v", err)
}
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
for _, fichier := range fichiers {
2024-07-18 07:56:04 +00:00
if filepath.Ext(fichier.Name()) == ".docx" {
2024-07-05 21:43:24 +00:00
cheminFichier := filepath.Join(dossier, fichier.Name())
doc, err := document.Open(cheminFichier)
if err != nil {
log.Printf("Erreur lors de l'ouverture du fichier %s : %v", fichier.Name(), err)
continue
}
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
for _, para := range doc.Paragraphs() {
newPara := docFusionne.AddParagraph()
2024-07-18 07:55:34 +00:00
// newPara.Properties().SetAlignment(para.Properties().Alignment())
2024-07-05 21:43:24 +00:00
for _, run := range para.Runs() {
newRun := newPara.AddRun()
newRun.Properties().SetBold(run.Properties().IsBold())
newRun.Properties().SetItalic(run.Properties().IsItalic())
2024-07-18 07:55:34 +00:00
// newRun.Properties().SetUnderline(run.Properties().IsUnderline())
2024-07-05 21:43:24 +00:00
newRun.AddText(run.Text())
}
2024-07-05 13:13:57 +00:00
}
2024-07-05 21:43:24 +00:00
doc.Close()
}
}
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
nomFichierSortie := "fusion.doc"
err = docFusionne.SaveToFile(nomFichierSortie)
if err != nil {
log.Fatalf("Erreur lors de l'enregistrement du document fusionné : %v", err)
}
2024-07-05 13:13:57 +00:00
2024-07-05 21:43:24 +00:00
fmt.Printf("Documents fusionnés dans %s\n", nomFichierSortie)
2024-07-05 13:13:57 +00:00
}