做php网站,濮阳网直播,王野摩托,网站后台管理需求文章目录 mammothdocx4js mammoth
官网地址#xff1a;https://github.com/mwilliamson/mammoth.js#readme
安装mammoth#xff1a;
npm i mammoth -S我们可以安装mammoth来实现上传的word文件的在线预览#xff0c;我们以element的上传组件为示例#xff1a;
temp… 文章目录 mammothdocx4js mammoth
官网地址https://github.com/mwilliamson/mammoth.js#readme
安装mammoth
npm i mammoth -S我们可以安装mammoth来实现上传的word文件的在线预览我们以element的上传组件为示例
templatedivel-upload classupload-demo drag action# :auto-uploadfalse :on-changechangeFileel-icon classel-icon--uploadupload-filled //el-icondiv classel-upload__textDrop file here or emclick to upload/em/divtemplate #tipdiv classel-upload__tipjpg/png files with a size less than 500kb/div/template/el-uploaddiv v-htmlwordcontent/div/div
/template
script langts setup
import { ref } from vue
import type { UploadProps } from element-plus
import mammoth from mammothconst wordcontent ref()const changeFile: UploadProps[onChange] (uploadFile, uploadFiles) {const file uploadFile.rawconst myReader new FileReader()myReader.readAsArrayBuffer(file as Blob)myReader.addEventListener(loadend, function (e) {const buffer e?.target?.result // arraybuffer objectmammoth.convertToHtml({arrayBuffer: buffer}).then(function (result: { value: string; messages: any[] }) {const html result.value // The generated HTMLconst messages result.messages // Any mesarnings during conversionwordcontent.value html}).done()})
}
/script
详细的API参考官网地址。
docx4js
官网地址https://github.com/lalalic/docx4js
使用docx4js实现获取docx文件有多少页码。
安装docx4js
npm i docx4js -S我们新建一个docx.mjs,代码如下
import docx4js from docx4js
import { TextDecoder } from utildocx4js.docx.load(./test2.docx).then((doc) {const propsAppRaw doc.parts[docProps/app.xml]._data.getContent()const propsApp new TextDecoder(utf-8).decode(propsAppRaw)console.log(propsApp)const match propsApp.match(/Pages(\d)\/Pages/)if (match match[1]) {const count Number(match[1])console.log(count)}
})执行命令 node docx.mjs
可以在控制台看到打印结果,第一个打印
?xml version1.0 encodingUTF-8 standaloneyes?
Properties xmlnshttp://schemas.openxmlformats.org/officeDocument/2006/extended-properties xmlns:vthttp://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypesTemplateNormal.dotm/TemplatePages4/PagesWords996/WordsCharacters1013/CharactersLines0/LinesParagraphs0/ParagraphsTotalTime0/TotalTimeScaleCropfalse/ScaleCropLinksUpToDatefalse/LinksUpToDateCharactersWithSpaces1250/CharactersWithSpacesApplicationWPS Office_12.1.0.18543_F1E327BC-269C-435d-A152-05C5408002CA/ApplicationDocSecurity0/DocSecurity/Properties第二个打印
4其中的4表示这个word文档是4页。
但是有时候有的word文档解析的结果是不一样的如下
?xml version1.0 encodingUTF-8 standaloneyes?
ap:Properties xmlns:vthttp://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes xmlns:aphttp://schemas.openxmlformats.org/officeDocument/2006/extended-properties/该文件中就没有包含Pages的字段。 所以综上所示如果通过docx4js获取word文件的页码数量是不准确的并且需要在node环境中运行且文件是docx格式的doc格式的不支持。