Created
December 6, 2020 00:50
-
-
Save rikkix/5e36f7aabebf6f4b726280d7197d137a to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"bytes" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"github.com/unidoc/unioffice/common" | |
"github.com/unidoc/unioffice/document" | |
"github.com/unidoc/unioffice/measurement" | |
) | |
type Question struct { | |
ID string | |
Qst string | |
A, B, C, D string | |
Pic string | |
} | |
func init() { | |
} | |
func NewQuestion(b []byte) *Question { | |
lines := bytes.Split(b, []byte("\n")) | |
q := Question{} | |
for i := range lines { | |
line := bytes.TrimSpace(lines[i]) | |
if bytes.HasPrefix(line, []byte("[I]")) { | |
q.ID = string(bytes.TrimPrefix(line, []byte("[I]"))) | |
} | |
if bytes.HasPrefix(line, []byte("[Q]")) { | |
q.Qst = string(bytes.TrimPrefix(line, []byte("[Q]"))) | |
} | |
if bytes.HasPrefix(line, []byte("[A]")) { | |
q.A = string(bytes.TrimPrefix(line, []byte("[A]"))) | |
} | |
if bytes.HasPrefix(line, []byte("[B]")) { | |
q.B = string(bytes.TrimPrefix(line, []byte("[B]"))) | |
} | |
if bytes.HasPrefix(line, []byte("[C]")) { | |
q.C = string(bytes.TrimPrefix(line, []byte("[C]"))) | |
} | |
if bytes.HasPrefix(line, []byte("[D]")) { | |
q.D = string(bytes.TrimPrefix(line, []byte("[D]"))) | |
} | |
if bytes.HasPrefix(line, []byte("[P]")) { | |
q.Pic = string(bytes.TrimPrefix(line, []byte("[P]"))) | |
} | |
} | |
return &q | |
} | |
func main() { | |
file, err := ioutil.ReadFile("B.txt") | |
if err != nil { | |
log.Panicln(err) | |
} | |
sections := bytes.Split(file, []byte("\n\n")) | |
sections = sections[1:] | |
var questions []*Question | |
for i := range sections { | |
question := NewQuestion(sections[i]) | |
questions = append(questions, question) | |
} | |
doc := document.New() | |
defer doc.Close() | |
for i := range questions { | |
para := doc.AddParagraph() | |
run := para.AddRun() | |
run.Properties().SetBold(true) | |
run.AddText(questions[i].Qst) | |
run.AddBreak() | |
run = para.AddRun() | |
run.AddText("A. ") | |
run.AddText(questions[i].A) | |
run.AddBreak() | |
run.AddText("B. ") | |
run.AddText(questions[i].B) | |
run.AddBreak() | |
run.AddText("C. ") | |
run.AddText(questions[i].C) | |
run.AddBreak() | |
run.AddText("D. ") | |
run.AddText(questions[i].D) | |
run.AddBreak() | |
if questions[i].Pic != "" { | |
imgData,err := ioutil.ReadFile(fmt.Sprintf("pic/%s",questions[i].Pic)) | |
if err != nil { | |
log.Panicln(err) | |
} | |
img, err := common.ImageFromBytes(imgData) | |
if err != nil { | |
log.Panicln(err) | |
} | |
ref, err := doc.AddImage(img) | |
if err != nil { | |
log.Panicln(err) | |
} | |
run = para.AddRun() | |
run.AddText("Pic: \n") | |
drawing, err := run.AddDrawingInline(ref) | |
if err != nil { | |
log.Panicln(err) | |
} | |
drawing.SetSize(4*measurement.Centimeter,4*measurement.Centimeter) | |
run.AddBreak() | |
} | |
} | |
doc.SaveToFile("simple.docx") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment