前端實現(xiàn)word文檔預(yù)覽和內(nèi)容提取的詳細過程
需求
上一篇寫了excel文檔解析,順便就看看word文檔。
解決問題
1.前端在瀏覽器預(yù)覽word文檔。
2.可以直接提取word文檔內(nèi)容
利用技術(shù)
預(yù)覽文檔--docx-preview
<script src="https://cdn.jsdelivr.net/npm/docx-preview@0.1.15/dist/docx-preview.js"></script>
提取文檔內(nèi)容--mammoth
<script src="https://cdn.bootcdn.net/ajax/libs/mammoth/1.6.0/mammoth.browser.min.js"></script>
預(yù)覽文檔實現(xiàn)過程
// HTML <div id="preview"></div>
實現(xiàn)預(yù)覽功能
renderAsync接收四個參數(shù)
1.document: 數(shù)據(jù)格式可以為Blob | ArrayBuffer | Uint8Array, // could be any type that supported by JSZip.loadAsync
2.bodyContainer: 渲染的區(qū)域
3.styleContainer: 通常用于指定一個HTML元素,該元素將用于包含和管理渲染文檔所需的樣式信息,包括字體、顏色、布局等。
4.options:{} 具體參數(shù)看文檔
// JavaScript
const onWord = (event) => {
let reader = new FileReader();
let file = event.target.files[0];
let options = { inWrapper: false, ignoreWidth: true, ignoreHeight: true }
docx.renderAsync(file, document.getElementById("preview"), null, options)
}提取word文檔實現(xiàn)過程
1.extractRawText--轉(zhuǎn)文字;
2.convertToHtml--轉(zhuǎn)HTML;
3.convertToMarkdown--轉(zhuǎn)Markdown 文檔
const onWord = (event) => {
let reader = new FileReader();
let file = event.target.files[0];
if (file) {
reader.onload = function (e) {
const data = e.target.result;
// 轉(zhuǎn)文字
mammoth.extractRawText({ arrayBuffer: data }).then(function (displayResult) {
wordData.value = displayResult.value
}).done();
// 轉(zhuǎn)HTML
mammoth.convertToHtml({ arrayBuffer: data }).then(function (displayResult) {
console.log(displayResult);
}).done();
// 轉(zhuǎn)Markdown 文檔。
mammoth.convertToMarkdown({ arrayBuffer: data }).then(function (displayResult) {
console.log(displayResult);
}).done();
};
reader.readAsArrayBuffer(file);
}
}word預(yù)覽

效果預(yù)覽

完整代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
<title>上傳word文件</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
padding: 0 50px;
}
.operation {
padding: 20px;
}
.btn {
min-width: 50px;
font-size: 20px;
color: #fff;
background: #118ee9;
margin: 0 20px 0 0;
padding: 8px;
border: none;
border-radius: 4px;
box-sizing: border-box;
}
.item {
width: 350px;
box-sizing: border-box;
padding: 4px 14px 4px 14px;
color: #000;
font-size: 12px;
background: #fff;
}
</style>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/mammoth/1.6.0/mammoth.browser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jszip@3.10.1/dist/jszip.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/docx-preview@0.1.15/dist/docx-preview.js"></script>
</head>
<body>
<div class="container" id="app">
<div class="operation">
<button class="btn" @click="uploadFile">上傳word文件</button>
</div>
<br>
<div v-if="wordData">{{wordData}}</div>
<br>
<div id="preview"></div>
</div>
<script>
const { createApp, ref, onMounted } = Vue
createApp({
setup() {
let wordData = ref('')
const uploadFile = (mark) => {
let inputEle = document.createElement('input')
inputEle.type = 'file'
inputEle.accept = '.doc,.docx,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
inputEle.click()
inputEle.addEventListener('input', (event) => {
onWord(event)
})
};
const onWord = (event) => {
let reader = new FileReader();
let file = event.target.files[0];
let options = { inWrapper: false, ignoreWidth: true, ignoreHeight: true }
docx.renderAsync(file, document.getElementById("preview"),null, options)
let fileName = file.name
if (file) {
reader.onload = function (e) {
const data = e.target.result;
// 轉(zhuǎn)文字
mammoth.extractRawText({ arrayBuffer: data }).then(function (displayResult) {
wordData.value = displayResult.value
}).done();
// 轉(zhuǎn)HTML
mammoth.convertToHtml({ arrayBuffer: data }).then(function (displayResult) {
console.log(displayResult);
}).done();
// 轉(zhuǎn)Markdown 文檔。
mammoth.convertToMarkdown({ arrayBuffer: data }).then(function (displayResult) {
console.log(displayResult);
}).done();
};
reader.readAsArrayBuffer(file);
}
}
return {
wordData,
uploadFile,
onWord,
}
}
}).mount('#app')
</script>
</body>
</html>總結(jié)
到此這篇關(guān)于前端實現(xiàn)word文檔預(yù)覽和內(nèi)容提取的文章就介紹到這了,更多相關(guān)前端word文檔預(yù)覽和內(nèi)容提取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IE6-IE9中tbody的innerHTML不能賦值的解決方法
這篇文章主要介紹了IE6-IE9中tbody的innerHTML不能賦值的解決方法,很實用,需要的朋友可以參考下2014-09-09
用瀑布流的方式在網(wǎng)頁上插入圖片的簡單實現(xiàn)方法
下面小編就為大家?guī)硪黄闷俨剂鞯姆绞皆诰W(wǎng)頁上插入圖片的簡單實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
Javascript中函數(shù)名.length屬性用法分析(對比arguments.length)
這篇文章主要介紹了Javascript中函數(shù)名.length屬性用法,結(jié)合實例形式簡單對比分析了與arguments.length屬性的用法區(qū)別,需要的朋友可以參考下2016-09-09

