Showing posts with label Word. Show all posts
Showing posts with label Word. Show all posts

Monday, June 20, 2011

Update an open XML word document in F#

 
I have created a small function in F# to update the content of a word document.
//reference to the package 
#r "WindowsBase"
//reference to the Xml and Linq 
#r "System.Xml"
#r "System.Xml.Linq"

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

//helper to create XName with a namespace
let xname str =
XName.Get(str, "http://schemas.openxmlformats.org/wordprocessingml/2006/main")

// the main function 
// changeDoc copies a word .docx document and updates the content by changing the xml that represents the content
let changeDoc oldPath newPath (updateDocument:XDocument -> XDocument) =
//update the package part
let updatePart (part:PackagePart) =
using (part.GetStream(FileMode.Open, FileAccess.Read)) (fun stream -> 
let xDocumentOut = stream |> XDocument.Load |> updateDocument
xDocumentOut.Save(part.GetStream(FileMode.Create, FileAccess.Write))
)

File.Copy(oldPath, newPath, true)
using (Package.Open(newPath, FileMode.Open, FileAccess.ReadWrite)) (fun package ->
let uri = new Uri("/word/document.xml", UriKind.Relative )

uri |> package.GetPart|> updatePart
)

// test function to update the document
// adds "_test" to every text  in the document.
let updateText (xDoc:XDocument) =
"t" |> xname  |> xDoc.Descendants |> Seq.iter (fun x -> x.Value <- x.Value + "_test")
xDoc

//test
changeDoc @"D:\Tmp\test.docx" @"D:\Tmp\test_copy.docx" updateText;;
Before "D:\Tmp\test.docx":
before
 
After "D:\Tmp\test_copy.docx":
after

Tuesday, June 7, 2011

Create Open XML Word document in F# (update)

This time I will create a word document (open xml) in F# without using the Open XML SDK and using XML libraries. Things have been intentionally kept simple.

I start by modelling a simple word document:

There are Two types of paragraphs: normal and bold ones:
  1. //reference to the package
  2. #r "WindowsBase"
  3.  
  4. open System
  5. open System.IO
  6. open System.IO.Packaging
  7.  
  8. type Paragraph =
  9.     | Normal of string
  10.     | Bold of string
 
And a document is a list of paragraphs:
  1. type Document =
  2.     | Paragraphs of Paragraph list
 
This is the test:
  1. //test
  2. let list = [Normal("test1"); Normal("test2"); Bold("test3"); Normal("test4")]
  3. let doc = Paragraphs(list)

 

Next we have the to create an xml version of document:






  1. //helpers

  2.  

  3. // http://weblogs.sqlteam.com/mladenp/archive/2008/10/21/Different-ways-how-to-escape-an-XML-string-in-C.aspx

  4. let encodedXml (text: string) =

  5.     text.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;")

  6.  

  7. let xml1 = "<node>it's my \"node\" & i like it<node>"

  8. let encode = encodedXml xml1

  9.  

  10. let addString acc item = acc + "\r\t" + item

  11.  

  12. let createParagraphNormal text =

  13.     "<w:p>

  14.       <w:r>

  15.         <w:t>" + encodedXml text +

  16.         "</w:t>

  17.       </w:r>

  18.     </w:p>"

  19.  

  20. let createParagraphBold text =

  21.     "<w:p>

  22.       <w:r>

  23.         <w:rPr>

  24.          <w:b />

  25.         </w:rPr>

  26.         <w:t>" + encodedXml text +

  27.         "</w:t>

  28.       </w:r>

  29.     </w:p>"

  30.  

  31. let createParagraph = function

  32.     | Normal(text) -> createParagraphNormal text

  33.     | Bold (text) -> createParagraphBold text

  34.  

  35. let createParagraphs = function

  36.     | Paragraphs(list) -> list|> List.map createParagraph |> List.reduce addString

  37.  

  38. //test

  39. let p = createParagraphs doc

  40.  

  41. let createDocument doc =

  42.     let startDocument = @"<?xml version=""1.0"" encoding=""utf-8""?>

  43.                     <w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">

  44.                       <w:body>"

  45.     let endDocument = "  </w:body>

  46.                         </w:document>"

  47.     let content = createParagraphs doc

  48.     startDocument + content + endDocument





 

Then we have to create a package, add the document and save it to disk:






  1. //add the document to package and save

  2. let createFile doc (fileName:string) =

  3.     using (Package.Open(fileName, FileMode.Create, FileAccess.ReadWrite))(fun package ->

  4.         let uri = new Uri("/word/document.xml", UriKind.Relative )

  5.         let partDocumentXML =

  6.             package.CreatePart(

  7.                                 uri,

  8.                                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" )

  9.     

  10.         using(new StreamWriter(partDocumentXML.GetStream(FileMode.Create, FileAccess.Write)))(fun stream ->

  11.             doc |> createDocument |> stream.Write

  12.         )

  13.  

  14.         package.CreateRelationship(

  15.                                     uri,

  16.                                     TargetMode.Internal,

  17.                                     "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument",

  18.                                     "rId1") |> ignore

  19.     )

  20.  

  21. //test

  22. let fileName = @"D:\Tmp\test.docx"

  23.  

  24. createFile doc fileName;;




 
This is the result:

 

I have created a map from twitter to my document model






  1. type UserStatus =

  2.     { UserName : string;

  3.       ProfileImage : string;

  4.       Status : string;

  5.       StatusDate : DateTime }

  6.  

  7. let mapUserStatus (us:UserStatus) =

  8.     let date = "Date: " + us.StatusDate.ToShortDateString()  + " time: " + us.StatusDate.ToLongTimeString()

  9.     [Normal(date); Bold(us.UserName); Normal(us.Status); Normal("")]

  10.  

  11. let addUserStatus acc item = item @ acc

  12.  

  13. let result = xml|> parseTweetXml |> List.map mapUserStatus|> List.reduce addUserStatus





 


This results in the following document:









Remark: I had to Google “Weiner” to understand the jokes.

Tuesday, May 24, 2011

Create Open XML Word document in F#

I needed a word document. I created it with the Open XML SDK and F# and posted the snippet at fssnip.net.

1: //reference to the Open Office SDK
2: #r @"C:\Program Files (x86)\Open XML SDK\V2.0\lib\DocumentFormat.OpenXml.dll"
3: //reference to the package
4: #r "WindowsBase"
5:
6: open DocumentFormat.OpenXml
7: open DocumentFormat.OpenXml.Wordprocessing
8: open DocumentFormat.OpenXml.Packaging
9:
10: let testString = "This is a test"
11: let printXml text =
12: printfn "xml: %s" text
13:
14: let createBody (text:string) =
15: let text = new Text(text)
16: let run = new Run()
17: run.AppendChild(text) |> ignore
18: let para = new Paragraph()
19: para.AppendChild(run)|> ignore
20: let body = new Body()
21: body.AppendChild(para)|> ignore
22: body
23:
24: printXml (createBody testString).InnerXml
25:
26: let createDocument (text:string) =
27: let body = createBody text
28: let doc = new Document()
29: doc.AppendChild(body) |> ignore
30: doc
31:
32: printXml (createDocument testString).InnerXml
33:
34: let createWordprocessingDocument (filepath:string) text=
35: using (WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document)) (fun doc ->
36: let mainPart = doc.AddMainDocumentPart();
37: mainPart.Document <- createDocument text
38: )
39:
40: let result3 = createWordprocessingDocument @"D:\Tmp\test1.docx" testString
val testString : string

Full name: Snippet.testString

type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
val printXml : string -> unit

Full name: Snippet.printXml
val text : string

type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
val printfn : Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val createBody : string -> 'a

Full name: Snippet.createBody
Multiple items
val string : 'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------

type string = System.String

Full name: Microsoft.FSharp.Core.string

type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
val text : 'a
namespace Microsoft.FSharp.Text
val run : 'a
val ignore : 'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val para : 'a
val body : 'a
val createDocument : string -> 'a

Full name: Snippet.createDocument
val doc : 'a
val createWordprocessingDocument : string -> 'a -> 'b

Full name: Snippet.createWordprocessingDocument
val filepath : string

type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
val using : 'T -> ('T -> 'U) -> 'U (requires 'T :> System.IDisposable)

Full name: Microsoft.FSharp.Core.Operators.using
val doc : 'a (requires 'a :> System.IDisposable)

type: 'a
implements: System.IDisposable
val mainPart : 'a
val result3 : 'a

Full name: Snippet.result3

It is a direct transformation of the C# code from the help file of the Open XML SDK:

public static void CreateWordprocessingDocument(string filepath)
{
    // Create a document by supplying the filepath. 
    using (WordprocessingDocument wordDocument =
        WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
    {
        // Add a main document part. 
        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
 
        // Create the document structure and add some text.
        mainPart.Document = new Document();
        Body body = mainPart.Document.AppendChild(new Body());
        Paragraph para = body.AppendChild(new Paragraph());
        Run run = para.AppendChild(new Run());
        run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));
    }
}

Total Pageviews