-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (138 loc) · 3.19 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"bytes"
"fmt"
"os"
wkhtmltopdf "github.com/SebastiaanKlippert/go-wkhtmltopdf"
"github.com/olekukonko/tablewriter"
"github.com/xuri/excelize/v2"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
//"bufio"
)
func main() {
f, err := excelize.OpenFile("Index SEC541.xlsx")
if err != nil {
fmt.Println(err)
return
}
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
for index, name := range f.GetSheetMap() {
data := [][]string{}
fmt.Println(index)
rows, err := f.GetRows(name)
if err != nil {
fmt.Println(err)
return
}
for i, row := range rows {
if i == 0 {
continue
}
rowData := []string{}
for j := 1; j <= 4; j++ {
if j < len(row) {
rowData = append(rowData, row[j])
} else {
rowData = append(rowData, "")
}
}
data = append(data, rowData)
}
fileName := fmt.Sprintf("Index SEC541 %v.md", name)
mkdoc, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
fmt.Println(err)
return
}
defer mkdoc.Close()
header := fmt.Sprintf("# %v\n", name)
_, err = mkdoc.WriteString(header)
if err != nil {
fmt.Println(err)
return
}
// Create a new table writer
table := tablewriter.NewWriter(mkdoc)
table.SetHeader([]string{"PAGE", "SLIDE TITLE", "KEYWORD 1", "KEYWORD 2"})
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetAutoWrapText(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
// Append the data rows to the table
for _, row := range data {
table.Append(row)
}
// Render the table
table.Render()
// Read the Markdown file
mdFile, err := os.ReadFile(fileName)
if err != nil {
fmt.Println(err)
return
}
// Convert markdown to HTML
var buf bytes.Buffer
md := goldmark.New(
goldmark.WithExtensions(extension.Table),
goldmark.WithRendererOptions(
html.WithUnsafe(),
),
)
if err := md.Convert(mdFile, &buf); err != nil {
fmt.Println(err)
return
}
html := buf.String()
// Create a new PDF generator
pdfg, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
fmt.Println(err)
return
}
// Set the HTML content with custom CSS styles
htmlWithStyles := fmt.Sprintf(`
<html>
<head>
<style>
table {
border-collapse: collapse;
width: 100%%;
}
th, td {
border: 1px solid black;
padding: 8px;
word-wrap: break-word;
white-space: normal;
}
</style>
</head>
<body>
%s
</body>
</html>
`, html)
pdfg.AddPage(wkhtmltopdf.NewPageReader(bytes.NewReader([]byte(htmlWithStyles))))
// Set the PDF file path
pdfFileName := fmt.Sprintf("Index SEC541 %v.pdf", name)
// Create a new file
file, err := os.Create(pdfFileName)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
// Set the output to the file
pdfg.SetOutput(file)
// Generate the PDF file
if err := pdfg.Create(); err != nil {
fmt.Println(err)
return
}
}
}