[精讚] [會員登入]
347

使用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

你可能感興趣的文章

[vue.js] 設定 content type 今天在wickt 端怎麼就是收不到vue.js 以post 傳過來的資料 找了好久才發現 application/jso

停止多執行緒 利用主程式呼叫多執行緒時, 要怎麼停止正在執行的多執行緒

Thread java thread

Spring Boot Maven wrappers maven, mvnw, build command

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

讀取特定資料夾下的xls檔 讀取特定資料夾下的xls檔

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

java-身份証字號驗証 FormatCheck.java public class FormatCheck { private volatile

[vue.js] 設定 content type 今天在wickt 端怎麼就是收不到vue.js 以post 傳過來的資料 找了好久才發現 application/jso

2018 hoc 掃地機器人 掃地機器人只能打掃沒有障礙物(桌椅、牆壁)的範圍,請寫程式控制機器人打掃餐廳的所有走道, 並在清掃完畢後回到充電器。

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

[web]空氣品質嵌入頁面語法 空氣品質, aqi, ajax