在打字稿中解析 .docx 文档

时间:2021-07-12 15:49:27

标签: javascript html xml parsing docx

我正在尝试使用打字稿在上传文件后以 html 格式向用户显示 word 文档的文本,但我无法找到一种方法来提取保留换行符的文档文本。现在我正在使用文本内容来获取原始文本,但我想保留内部间距。我该怎么做呢?我的代码如下:

if (file.type.includes('word')) {
                    const new_zip = new JSzip();
                    new_zip.loadAsync(file).then( (zip) => {
                        return zip;
                    }).then( (zip) => {
                        const contentPromise = zip.file('word/document.xml').async('string').then( (content) => {
                            const lineXML = (new DOMParser()).parseFromString(content.toString(), 'application/xml');
                            const xml = lineXML.documentElement;
                            this.uploadedFiles[i].fileText = xml.textContent;
                            return content;
                        });
                    });
                }
            };

0 个答案:

没有答案