-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
142 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/example/ | ||
/tmp/ |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// ------------------------------------------------- | ||
// Package docx_parser | ||
// Author: hanzhi | ||
// Date: 2024/12/22 | ||
// ------------------------------------------------- | ||
|
||
package docx_parser |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// ------------------------------------------------- | ||
// Package docx_parser | ||
// Author: hanzhi | ||
// Date: 2024/12/22 | ||
// ------------------------------------------------- | ||
|
||
package docx_parser | ||
|
||
import ( | ||
"archive/zip" | ||
"encoding/xml" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
func ReadStyle(r *zip.ReadCloser, filePath string) (*Styles, error) { | ||
// 查找 document.xml.rels文件,也就是多媒体依赖 | ||
var styleFileRels *zip.File | ||
for _, f := range r.File { | ||
if f.Name == "word/styles.xml" { | ||
styleFileRels = f | ||
break | ||
} | ||
} | ||
|
||
if styleFileRels == nil { | ||
return nil, fmt.Errorf("styles.xml not found in %s", filePath) | ||
} | ||
// 读取style.xml的内容 | ||
rcDFR, err := styleFileRels.Open() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func(rc io.ReadCloser) { | ||
err := rc.Close() | ||
if err != nil { | ||
// empty | ||
} | ||
}(rcDFR) | ||
// 解析 | ||
var stylesList Styles | ||
err = xml.NewDecoder(rcDFR).Decode(&stylesList) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &stylesList, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// ------------------------------------------------- | ||
// Package docx_parser | ||
// Author: hanzhi | ||
// Date: 2024/12/22 | ||
// ------------------------------------------------- | ||
|
||
package docx_parser | ||
|
||
func stylizedBody(body *Body, styles *Styles) { | ||
// 根据styles生成一个map方便我查找 | ||
var styleFZMap map[string]int = make(map[string]int) | ||
for _, style := range styles.StyleList { | ||
if style.StyleId != "" { | ||
styleFZMap[style.StyleId] = style.FontSize.Value | ||
} | ||
} | ||
//fmt.Println(styleFZMap) | ||
// 遍历body,寻找paragraph | ||
for i, content := range body.Contents { | ||
if content.Type == "paragraph" { | ||
paragraph := content.Value.(Paragraph) | ||
if paragraph.StyleId.Value != "" { | ||
if fontSize, exists := styleFZMap[paragraph.StyleId.Value]; exists { | ||
for j := range paragraph.Runs { | ||
paragraph.Runs[j].FontSize.Value = fontSize | ||
} | ||
} | ||
} | ||
// 写回到 body.Contents[i] | ||
body.Contents[i].Value = paragraph | ||
} | ||
} | ||
|
||
//fmt.Println(styleFZMap) | ||
} |