使用JavaScript,我可以上传一个word文件并使用.replaces然后另存为新文档吗

Using JavaScript, Can I upload a word file and use .replace then save as new document

本文关键字:然后 replaces 另存为 文档 新文档 word 我可以 JavaScript 使用 一个 文件      更新时间:2023-12-16

使用JavaScript,我想上传一个word文档和/或在本地机器上浏览文件并查看内容。。。然后我想用不同的文本替换内容。

这是我想要使用的文本替换的一个片段。

<button onclick="myFunction()">Convert</button>
<script>
function myFunction()
{
var str = document.getElementById("source").value; 
    var res = 
     str.replace(/a/g, "ა") 
        .replace(/b/g, "ბ") 
        .replace(/g/g, "გ") 
        //+ more letters for entire alphabet
document.getElementById("source").value=res;
}
</script>

我想知道的是,是否可以获取word文档文件的内容,将所有字母更改为格鲁吉亚字符(同时尽可能保留格式),然后保存为新的word文档?

对于docx,您可以使用docx.jshttps://github.com/stephen-hardy/DOCX.js

如果使用.docx文件,这应该是可能的,因为docx是XML。您可能想要使用jQuery XML解析器(http://api.jquery.com/jQuery.parseXML/)或者获取XML字符串形式的文档。对于较大的文档,这可能不是最佳解决方案。