提取docx文件中的文本 [英] Extracting the text inside a docx file

查看:146
本文介绍了提取docx文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码读取.docx文件,它已成功从文件中提取文本.但是这里的问题是,它只是提取文本.例如,如果我的文档数据如下

I am using the below code to read .docx file and it is successfully extracting the text from the file. But the problem here is, it is just extracting the text. For example if my document data is like below

I am line 1

I am line 2      I am some other text

然后它像

I am line 1I am line 2I am some other text.

我只是想要原样.我怎样才能做到这一点.下面是我现在使用的代码.

I just want as it is. How can I do that. Below is the code I am using now.

 open System
 open System.IO
 open System.IO.Packaging
 open System.Xml

 let getDocxContent (path: string) =
 use package = Package.Open(path, FileMode.Open)
 let stream = package.GetPart(new Uri("/word/document.xml",UriKind.Relative)).GetStream()
 stream.Seek(0L, SeekOrigin.Begin) |> ignore
 let xmlDoc = new XmlDocument()
 xmlDoc.Load(stream)
 xmlDoc.DocumentElement.InnerText
 let docData = getDocxContent @"C:\a1.docx"
 printfn "%s" docData

推荐答案

您需要设置因此,请更改以下代码:

So change the code from:

let xmlDoc = new XmlDocument()
xmlDoc.Load(stream)

收件人:

let xmlDoc = new XmlDocument()
xmlDoc.PreserveWhitespace <- true
xmlDoc.Load(stream)

这篇关于提取docx文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆