[精讚] [會員登入]
412

使用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 jpa 使用多個欄位排序 我有一entity 叫 team 資料欄下如下 @Id @GeneratedValue(strategy = Gener

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

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

Thread java thread

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

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

vue.js components 多個組件的呈現 vue.js 組件 component

vue.js component 在parent與child 傳值 component 在parent與child 傳值

資料表更改為多個primary key, MariaDB [database]> describe TABLENAME; 想由本來是兩個PRIMARY KE

proxmox lxc 救援 今天突然接到一名強者我朋友的臨時求援 他說他把pve 從5.1升到5.2後, kvm的虛擬机器都沒問題 , 但是lxc的

hoc2018灑水機器人 灑水機器人的工作是替行道樹灑水,機器人的灑水範圍有限(左前方、左方、左後方),請寫程式控制機器 人判斷須灑水的狀況。每顆