使用 javascript/jquery 获取 docx 文件内容
get docx file contents using javascript/jquery
希望使用客户端技术打开/读取 docx 文件 (HTML/JS)。
如果可能,请提供帮助。找到了一个名为 docx.js 的 Javascript 库,但个人似乎无法找到它的任何文档。
(http://blog.innovatejs.com/?p=184)
目标是为 docx 文件和 txt 文件制作一个基于浏览器的搜索工具。
感谢任何帮助。
使用 docxtemplater,您可以使用 doc.getFullText() 方法轻松获取单词的全文(仅适用于 docx)。
HTML代码:
<body>
<button onclick="gettext()">Get document text</button>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/docxtemplater/3.26.2/docxtemplater.js"></script>
<script src="https://unpkg.com/pizzip@3.1.1/dist/pizzip.js"></script>
<script src="https://unpkg.com/pizzip@3.1.1/dist/pizzip-utils.js"></script>
<script>
function loadFile(url, callback) {
PizZipUtils.getBinaryContent(url, callback);
}
function gettext() {
loadFile(
"https://docxtemplater.com/tag-example.docx",
function (error, content) {
if (error) {
throw error;
}
var zip = new PizZip(content);
var doc = new window.docxtemplater(zip);
var text = doc.getFullText();
console.log(text);
alert("Text is " + text);
}
);
}
</script>
如果您希望能够在网络浏览器中显示 docx 文件,您可能会对 Native Documents 最近发布的商业 Word 文件编辑器感兴趣;在 https://nativedocuments.com/test_drive.html
试试
如果您这样做,您将获得比尝试转换为 (X)HTML 并以这种方式查看更好的布局保真度。
它专为嵌入网络应用程序而设计,因此有一个 API 用于加载文档,并且它会愉快地位于您的网络应用程序的安全上下文中。
披露:我对原生文档有商业兴趣
我知道这是一个旧的 post,但是 doctemplater 已经改变,接受的答案不再有效。这对我有用:
function loadDocx(filename) {
// Read document.xml from docx document
const AdmZip = require("adm-zip");
const zip = new AdmZip(filename);
const xml = zip.readAsText("word/document.xml");
// Load xml DOM
const cheerio = require('cheerio');
$ = cheerio.load(xml, {
normalizeWhitespace: true,
xmlMode: true
})
// Extract text
let out = new Array()
$('w\:t').each((i, el) => {
out.push($(el).text())
})
return out
}
希望使用客户端技术打开/读取 docx 文件 (HTML/JS)。
如果可能,请提供帮助。找到了一个名为 docx.js 的 Javascript 库,但个人似乎无法找到它的任何文档。 (http://blog.innovatejs.com/?p=184)
目标是为 docx 文件和 txt 文件制作一个基于浏览器的搜索工具。
感谢任何帮助。
使用 docxtemplater,您可以使用 doc.getFullText() 方法轻松获取单词的全文(仅适用于 docx)。
HTML代码:
<body>
<button onclick="gettext()">Get document text</button>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/docxtemplater/3.26.2/docxtemplater.js"></script>
<script src="https://unpkg.com/pizzip@3.1.1/dist/pizzip.js"></script>
<script src="https://unpkg.com/pizzip@3.1.1/dist/pizzip-utils.js"></script>
<script>
function loadFile(url, callback) {
PizZipUtils.getBinaryContent(url, callback);
}
function gettext() {
loadFile(
"https://docxtemplater.com/tag-example.docx",
function (error, content) {
if (error) {
throw error;
}
var zip = new PizZip(content);
var doc = new window.docxtemplater(zip);
var text = doc.getFullText();
console.log(text);
alert("Text is " + text);
}
);
}
</script>
如果您希望能够在网络浏览器中显示 docx 文件,您可能会对 Native Documents 最近发布的商业 Word 文件编辑器感兴趣;在 https://nativedocuments.com/test_drive.html
试试如果您这样做,您将获得比尝试转换为 (X)HTML 并以这种方式查看更好的布局保真度。
它专为嵌入网络应用程序而设计,因此有一个 API 用于加载文档,并且它会愉快地位于您的网络应用程序的安全上下文中。
披露:我对原生文档有商业兴趣
我知道这是一个旧的 post,但是 doctemplater 已经改变,接受的答案不再有效。这对我有用:
function loadDocx(filename) {
// Read document.xml from docx document
const AdmZip = require("adm-zip");
const zip = new AdmZip(filename);
const xml = zip.readAsText("word/document.xml");
// Load xml DOM
const cheerio = require('cheerio');
$ = cheerio.load(xml, {
normalizeWhitespace: true,
xmlMode: true
})
// Extract text
let out = new Array()
$('w\:t').each((i, el) => {
out.push($(el).text())
})
return out
}