[精讚] [會員登入]
445

使用poi 解析 docx

原先是想解析出在docx 中的文字跟圖片, 但是, 有些我們認為是圖片, 其實是用方程式表示, 實在是太麻煩了 就記錄一

分享此文連結 //n.sfs.tw/15284

分享連結 使用poi 解析 docx@igogo
(文章歡迎轉載,務必尊重版權註明連結來源)
2021-07-11 15:51:59 最後編修
2021-07-09 14:07:55 By igogo
 

 

原先是想解析出在docx 中的文字跟圖片, 

但是, 有些我們認為是圖片, 其實是用方程式表示,  實在是太麻煩了

就記錄一下怎麼使用 poi 來解析docx

 

https://poi.apache.org/

https://poi.apache.org/components/document/quick-guide-xwpf.html

docx 使用xwpf 來解析

 

取得所有在doc下的 docx檔

 File docx = new File("doc");
        //get docx files
        List<Path> docxfiles = new ArrayList<>();
        docxfiles = Files.list(Path.of(docx.toURI()))
                .filter(file -> file.toString().endsWith("docx"))
                .collect(Collectors.toList());

 

讀出文字

XWPFDocument document = new XWPFDocument(Files.newInputStream(docxfile));

List<XWPFParagraph> paragraphs = document.getParagraphs();

paragraphs.forEach(paragraph -> {
     String text = paragraph.getText();

});

 

 

讀出圖片,  這是讀取在XWPFParagraph的語法,  並轉成base64 這樣才知道圖片屬於哪一段

XWPFDocument document = new XWPFDocument(Files.newInputStream(docxfile));

List<XWPFParagraph> paragraphs = document.getParagraphs();

paragraphs.forEach(paragraph -> {
       List<XWPFRun> runs = paragraph.getParagraph().getRuns();
            runs.forEach(run -> {
                if (run.getEmbeddedPictures().size() > 0) {

                    run.getEmbeddedPictures().forEach(xwpfPicture -> {
                        byte[] bytes = xwpfPicture.getPictureData().getData();
                        String encode = Base64.getEncoder().encodeToString(bytes);
                    
                    });
                }
            });

});

 

 

 

 

 

 

 

 

END

你可能感興趣的文章

keycloak 透過java client lib新增user 使用keycloak-admin-client lib maven <dependency> <gro

ArrayList 與 HashMap 範例 public static void main(String[] args) { String titleIds = &

spring boot 使用jdbc連接mariadb spring boot, mariadb, jdbc, rowmapper

利用maven建立一個可執行的jar檔 利用maven建立一個可執行的jar檔

ArrayList 想移除特定值 想移出water, 使用lambda 的方式如下 List<String> fruits = new Arr

刪除資料夾內的檔案 lambda 一行文 Arrays.stream(new File("/folder/path").

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

apache 反向代理 80轉443轉8080 apache, proxy pass

java.time 時間 instant java.time

vim 特定範圍行數開頭加上# 註解 vim 特定範圍行數開頭加上# 註解

windows ad 如何得知 dn 值 如何得知 windows ad 上的使用者dn 值 https://support.symantec.com/en_US

臺中市校務雲端系統與Windows AD帳號整合(1) active directory,ldaps,雲端校務系統