【问题标题】:Read contents of a DOC (.docx or .doc) file and convert it to String读取 DOC(.docx 或 .doc)文件的内容并将其转换为字符串
【发布时间】:2018-05-13 14:35:05
【问题描述】:

在阅读 DOC(.docx 或 .doc)文件的内容并将其显示在 UILabel 中时,我需要帮助。该文件来自 URL,因此我将文件下载到 DocumentsDirectory 并在寻找读取内容的解决方案的过程中。

我正在使用Alamofire.download 并且我已致电:

let content = try? String(contentsOfFile: (response.destinationURL?.path)!, encoding: String.Encoding.utf8)

但它正在返回nil

有谁知道如何阅读 doc 文件的内容,将不胜感激。

【问题讨论】:

  • 尝试将String.Encoding.utf8替换为String.Encoding.ascii
  • @ a.masri我用.ascii,我得到了很多字符,如ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ\ u {01} \0þÿ\ u {03} \ n \ 0 \0ÿÿÿÿ\ u {06} \ t \u{02
  • @LawGimenez :并非所有文件都可以“翻译”为字符串。只是尝试将 UIImage 读取为字符串,你真的做不到。您可以打印十六进制数据,但并非如此。 .doc 和 .docx 是私有格式。您可能会为此找到库(如果您想要好的库,则可能需要为此付费)。通常一个 UIWebView 可以渲染它们。

标签: ios swift docx doc


【解决方案1】:

你可以用这个SNDocx

做到这一点并不像你想象的那么容易, docx 文件是 XML 和其他文件的压缩集合。您无法将 docx 文件加载到 String 中。您需要使用Data 来加载zip 内容。然后你需要unzip它。然后您需要浏览所有文件并找到所需的word/document.xml,然后读取 xml 并解析。

我用Zippy

看这段代码

 guard let originalFileURL = Bundle.main.url(forResource: "test", withExtension: "docx") else {
            print("file not found :( ")
            return
        }
     do{

           let filename = try! ZipFile.init(url: originalFileURL)
            // file name  content 
            //            - 0 : "[Content_Types].xml"
            //            - 1 : "word/numbering.xml"
            //            - 2 : "_rels/.rels"
            //            - 3 : "word/theme/theme1.xml"
            //            - 4 : "word/fontTable.xml"
            //            - 5 : "word/document.xml"
            //            - 6 : "word/settings.xml"
            //            - 7 : "word/styles.xml"
            //            - 8 : "word/_rels/document.xml.rels"

            for file in filename {
                if file.contains("document.xml"){
                    let data = filename[file]
                    print(String.init(data: data!, encoding: String.Encoding.utf8))
                }
            }

        }catch{
            print(error)
        }

输出

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r<w:document xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\" xmlns:sl=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" xmlns:lc=\"http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas\" xmlns:dgm=\"http://schemas.openxmlformats.org/drawingml/2006/diagram\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\" xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\"><w:body><w:p w:rsidR=\"00000000\" w:rsidDel=\"00000000\" w:rsidP=\"00000000\" w:rsidRDefault=\"00000000\" w:rsidRPr=\"00000000\" w14:paraId=\"00000000\"><w:pPr><w:contextualSpacing w:val=\"0\"/><w:rPr/></w:pPr><w:r w:rsidDel=\"00000000\" w:rsidR=\"00000000\" w:rsidRPr=\"00000000\"><w:rPr><w:rtl w:val=\"0\"/></w:rPr><w:t xml:space=\"preserve\">test</w:t></w:r></w:p><w:sectPr><w:pgSz w:h=\"15840\" w:w=\"12240\"/><w:pgMar w:bottom=\"1440\" w:top=\"1440\" w:left=\"1440\" w:right=\"1440\" w:header=\"0\"/><w:pgNumType w:start=\"1\"/></w:sectPr></w:body></w:document>

你必须解析这个xml,你会在我的输出中发现它必须被解析,直到获得这个值

<w:t xml:space=\"preserve\">test</w:t>

docx XML Format reference

【讨论】:

  • 你能向我解释一下抛出错误是由哪一行代码造成的吗?
  • 可能是因为文件是doc文档而不是docx
  • 让我检查一下
  • @LawGimenez 很抱歉,但是向我解释 doc 和 docx 之间的区别时,您无法阅读 doc。您必须将 doc 转换为 docx,以便我们可以阅读内容。
  • 是的,这就是我的想法,一旦我使用 docx 确认,我会接受这个答案
猜你喜欢
  • 2015-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2011-06-14
  • 2011-10-03
相关资源
最近更新 更多