[精讚] [會員登入]
220

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

你可能感興趣的文章

Spring Boot Maven wrappers maven, mvnw, build command

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

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

Thread java thread

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

spring boot jpa 使用多個欄位排序 我有一entity 叫 team 資料欄下如下 @Id @GeneratedValue(strategy = Gener

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

tmux 將終端機切割成多個視 窗 tmux, multiple terminal

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

找尋多個文件夾中最新檔案(jpg,png,txt)並複製到nexus資料夾 找尋多個文件夾中最新檔案(jpg,png,txt)並複製到集中資料夾

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

臺中市雲端校務系統與Windows AD帳號整合(5) 安裝java環境