I need help on reading a DOC (.docx or .doc) file's contents and display it inside the UILabel. The file is from the URL, so I download the file to DocumentsDirectory and in the process of finding a solution to read the contents.
I am using Alamofire.download
and I have called:
let content = try? String(contentsOfFile: (response.destinationURL?.path)!, encoding: String.Encoding.utf8)
but it is returning nil
Does anyone know how to read the contents of a doc file would be appreciated.
You can use this SNDocx
OR
Doing this is not as easy as you imagine,
a docx file is a zipped up collection of XML and other files. You can't load a docx file into an String
. You would need to use Data
to load the zip
contents. Then you would need to unzip
it. Then you would need to go through all of the files and find the desired word/document.xml
then read xml and parse .
Im use Zippy
Look this code
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)
}
Output
<?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>
You have to parse xml this and you will find in the my output that it must be parse until this value is obtained
<w:t xml:space=\"preserve\">test</w:t>