[精讚] [會員登入]
396

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

你可能感興趣的文章

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

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

Thread java thread

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

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

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

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

centos 7 移机出現 dracut-initqueue timeout centos 7 移机出現 dracut-initqueue timeout 處理

臺中市雲端校務系統與Windows AD帳號整合(3) LDAP的基本概念

雲端校務系統與OPENLDAP帳號整合(3) 二 建置更改密碼服務 下載校端更改密碼程式: 連結 檢查是否安裝java 8 #java -version 安裝open

台中市網路應用競賽試場安排(一) 排試場就跟排班或是排課表一樣, 找不到一個都能讓所有人滿意的結果

台中市校園空氣品質預警 aqi