使用 Office JS 从 Word 文档的文本框中提取文本 Api
Extract text from a Text Box of a Word document using Office JS Api
我在 word 文档中有一些文本框(形状 > 文本框)。该文档是一个简历模板,其中包含很多内容。我想 select 文档的所有文本框,提取文本,删除文本框并注入提取的文本。我试过了
const range = context.document.getSelection();
range.load("text");
然后同步上下文以便我可以获取文本。
我最终采取了以下解决方法。它速度快,在 Windows 和 macOS
中都运行良好
获取文档正文的 OOXML
解析OOXL.value并生成xml文档(xml文档)
检测包含文本的现有文本框和形状:getElementsByTagName("wps:wsp")
从 (3)
中提取文本
生成一个简单的 xml TextElement 并提取文本
将 (3) 替换为 (5)
序列化为 xmlString 更新的 xmlDoc 并获取更新的 OOXML.value
将更新的 OOXML.value 插入文档以替换现有的
Word.run(函数(上下文){
//Select document body and extract OOXML
const body = context.document.body;
const ooxml = body.getOoxml();
return context.sync().then(function () {
//Initialize DOM Parser
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(ooxml.value, "text/xml");
//Get all runs
const rows = xmlDoc.getElementsByTagName("w:r");
for (let j = 0; j < rows.length; j++) {
const row = rows[j];
const rowHasTextBox = row.getElementsByTagName("wps:txbx").length > 0;
//If no textbox, shape, wordart exists skip current run
if (!rowHasTextBox) continue;
//Select textbox, shape, wordart and get paragraphs
const textboxContainer = row.getElementsByTagName("wps:txbx")[0];
const paragraphs = textboxContainer.getElementsByTagName("w:p");
// Create a new run which will replace the existing run
const newRow = xmlDoc.createElement("w:r");
const breakLine = xmlDoc.createElement("w:br");
//Append breakline and "{{"
newRow.appendChild(breakLine);
newRow.appendChild(startRow);
for (let p = 0; p < paragraphs.length; p++) {
//Check whether paragrapj has text
const paragraphHasText = paragraphs[p].getElementsByTagName("w:t").length > 0;
if (!paragraphHasText) continue;
//Extract text
let textExtracted = "";
const textBoxTexts = paragraphs[p].getElementsByTagName("w:t");
for (let k = 0; k < textBoxTexts.length; k++) {
const textBoxText = textBoxTexts[k].innerHTML;
textExtracted = textExtracted + textBoxText;
textExtracted = textExtracted + " ";
}
// Create a temp run which will hold the etxtracted text
const tempRow = xmlDoc.createElement("w:r");
const newText = xmlDoc.createElement('w:t');
newText.setAttribute("xml:space", "preserve");
newText.innerHTML = textExtracted;
textExtracted = "";
tempRow.appendChild(newText);
newRow.appendChild(tempRow);
const breakLine = xmlDoc.createElement("w:br");
newRow.appendChild(breakLine);
}
//Replace existing run with the new one
row.replaceWith(newRow);
}
//Serialize dom , clear body and replace OOXML
const serializedXML = new XMLSerializer().serializeToString(xmlDoc.documentElement);
body.clear();
return context.sync().then(function () {
body.insertOoxml(serializedXML, Word.InsertLocation.replace);
console.log('done');
});
});
})
.catch(error => {
console.log('Error: ', error);
resolve(false);
});
我在 word 文档中有一些文本框(形状 > 文本框)。该文档是一个简历模板,其中包含很多内容。我想 select 文档的所有文本框,提取文本,删除文本框并注入提取的文本。我试过了
const range = context.document.getSelection();
range.load("text");
然后同步上下文以便我可以获取文本。
我最终采取了以下解决方法。它速度快,在 Windows 和 macOS
中都运行良好获取文档正文的 OOXML
解析OOXL.value并生成xml文档(xml文档)
检测包含文本的现有文本框和形状:getElementsByTagName("wps:wsp")
从 (3)
中提取文本生成一个简单的 xml TextElement 并提取文本
将 (3) 替换为 (5)
序列化为 xmlString 更新的 xmlDoc 并获取更新的 OOXML.value
将更新的 OOXML.value 插入文档以替换现有的
Word.run(函数(上下文){
//Select document body and extract OOXML const body = context.document.body; const ooxml = body.getOoxml(); return context.sync().then(function () { //Initialize DOM Parser const parser = new DOMParser(); const xmlDoc = parser.parseFromString(ooxml.value, "text/xml"); //Get all runs const rows = xmlDoc.getElementsByTagName("w:r"); for (let j = 0; j < rows.length; j++) { const row = rows[j]; const rowHasTextBox = row.getElementsByTagName("wps:txbx").length > 0; //If no textbox, shape, wordart exists skip current run if (!rowHasTextBox) continue; //Select textbox, shape, wordart and get paragraphs const textboxContainer = row.getElementsByTagName("wps:txbx")[0]; const paragraphs = textboxContainer.getElementsByTagName("w:p"); // Create a new run which will replace the existing run const newRow = xmlDoc.createElement("w:r"); const breakLine = xmlDoc.createElement("w:br"); //Append breakline and "{{" newRow.appendChild(breakLine); newRow.appendChild(startRow); for (let p = 0; p < paragraphs.length; p++) { //Check whether paragrapj has text const paragraphHasText = paragraphs[p].getElementsByTagName("w:t").length > 0; if (!paragraphHasText) continue; //Extract text let textExtracted = ""; const textBoxTexts = paragraphs[p].getElementsByTagName("w:t"); for (let k = 0; k < textBoxTexts.length; k++) { const textBoxText = textBoxTexts[k].innerHTML; textExtracted = textExtracted + textBoxText; textExtracted = textExtracted + " "; } // Create a temp run which will hold the etxtracted text const tempRow = xmlDoc.createElement("w:r"); const newText = xmlDoc.createElement('w:t'); newText.setAttribute("xml:space", "preserve"); newText.innerHTML = textExtracted; textExtracted = ""; tempRow.appendChild(newText); newRow.appendChild(tempRow); const breakLine = xmlDoc.createElement("w:br"); newRow.appendChild(breakLine); } //Replace existing run with the new one row.replaceWith(newRow); } //Serialize dom , clear body and replace OOXML const serializedXML = new XMLSerializer().serializeToString(xmlDoc.documentElement); body.clear(); return context.sync().then(function () { body.insertOoxml(serializedXML, Word.InsertLocation.replace); console.log('done'); }); }); }) .catch(error => { console.log('Error: ', error); resolve(false); });