Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to read and write docx files in Qt
Forum Updated to NodeBB v4.3 + New Features

How to read and write docx files in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
35 Posts 9 Posters 27.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    Hi and welcome to devnet,

    You are trying to read a binary file as text and then write that text again and it will be again read as binary. That wont work.

    If you just want to copy the file, why not use one of QFile::copy overloads ?

    D Offline
    D Offline
    Dante Pham
    wrote on last edited by
    #3

    @SGaist Actually I'm copying multiple files and put it into one. Is there any way I can do that ?

    jsulmJ 1 Reply Last reply
    0
    • D Dante Pham

      @SGaist Actually I'm copying multiple files and put it into one. Is there any way I can do that ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Dante-Pham You want to combine multiple docx files into one? It is not possible with Qt. It will for sure not work if you just append them, even not in binary mode. You either need a library which understands docx format or you need to write docx parser by yourself.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Dante-Pham You want to combine multiple docx files into one? It is not possible with Qt. It will for sure not work if you just append them, even not in binary mode. You either need a library which understands docx format or you need to write docx parser by yourself.

        D Offline
        D Offline
        Dante Pham
        wrote on last edited by
        #5

        @jsulm Do you know any other platform like Qt that can read and write multiple docx files like what i mentioned ?

        jsulmJ 1 Reply Last reply
        0
        • D Dante Pham

          @jsulm Do you know any other platform like Qt that can read and write multiple docx files like what i mentioned ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #6

          @Dante-Pham No I don't. Maybe Microsoft provides something? It is their format. And you can take a look at LibreOffice - it can read/write docx.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #7

            docx is a zip file so you can understand how your method cannot possibly work.
            There is currently no free library in C++ that lets you handle it (a few commercial ones claim to be able to but I never tested them).
            The LibreOffice/OpenOffice code to handle MS office files is too linked to the application to be used easily.
            There's a .Net library that lets you handle them: https://github.com/WordDocX/DocX so you can use it with C++/CLI if you are ok being tied to .Net

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            D 1 Reply Last reply
            0
            • VRoninV VRonin

              docx is a zip file so you can understand how your method cannot possibly work.
              There is currently no free library in C++ that lets you handle it (a few commercial ones claim to be able to but I never tested them).
              The LibreOffice/OpenOffice code to handle MS office files is too linked to the application to be used easily.
              There's a .Net library that lets you handle them: https://github.com/WordDocX/DocX so you can use it with C++/CLI if you are ok being tied to .Net

              D Offline
              D Offline
              Dante Pham
              wrote on last edited by
              #8

              @VRonin Would you mind telling me exactly how to use this ? I'm not very familiar with .NET.

              mrjjM 1 Reply Last reply
              0
              • D Dante Pham

                @VRonin Would you mind telling me exactly how to use this ? I'm not very familiar with .NET.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #9

                @Dante-Pham
                Hi. Even with DocX you will need to understand the actual structure of the document and
                merge it by hand adding text to the target doc.
                There is no tool i have ever seen that can BINARY merge word docs. (only in very old times)
                Also note that DocX only goes to word 2007. Not newer. ( or docs not updated)
                But DocX seems very high level.

                I have never used .NET from c++. Im sure its pretty easy in Visual Studio but cant confirm.

                Can I ask why you want to merge them ?
                Also is it a requirement that your app can do it when no Office installed?

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Dante Pham
                  wrote on last edited by
                  #10

                  @mrjj Basically I'm trying to make a program that can copy lots of small documents and put it into just one file. It's like you have multiple questions and you merge them into just one test.
                  Office should always be installed with this program ( except for when it has already been installed )

                  1 Reply Last reply
                  1
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    Ok. i see.
                    Would it be option to do it via pdf or must end result be a doc?

                    I did something the same with
                    https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
                    (the free command line version)
                    so made small VBA macro that printed/saved the docs to pdf and then just
                    ran tool on it. and got perfect pdf from it.

                    D 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      Ok. i see.
                      Would it be option to do it via pdf or must end result be a doc?

                      I did something the same with
                      https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
                      (the free command line version)
                      so made small VBA macro that printed/saved the docs to pdf and then just
                      ran tool on it. and got perfect pdf from it.

                      D Offline
                      D Offline
                      Dante Pham
                      wrote on last edited by
                      #12

                      @mrjj Input and Output should be .doc or .docx so that it will be easier for users to modify the files. Changing .doc to .pdf and vice versa is good but doesn't help very much because the core features I intend to implement will, in most cases, involve in working with .doc and .docx

                      mrjjM 1 Reply Last reply
                      0
                      • D Dante Pham

                        @mrjj Input and Output should be .doc or .docx so that it will be easier for users to modify the files. Changing .doc to .pdf and vice versa is good but doesn't help very much because the core features I intend to implement will, in most cases, involve in working with .doc and .docx

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @Dante-Pham
                        Ok. well DocX or something like it seems the way to go then.

                        There is also the option of a pure VBA project. But the widgets you
                        get there is not as advanced as Qt versions but the merging of docs would
                        be easy.

                        D 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @Dante-Pham
                          Ok. well DocX or something like it seems the way to go then.

                          There is also the option of a pure VBA project. But the widgets you
                          get there is not as advanced as Qt versions but the merging of docs would
                          be easy.

                          D Offline
                          D Offline
                          Dante Pham
                          wrote on last edited by
                          #14

                          @mrjj After doing some research I think DocX may just work. However, I have never had any exprience dealing with .dll files, let alone using it in Qt. Do you have any suggestions on how to use it ?

                          mrjjM 1 Reply Last reply
                          0
                          • Andy314A Offline
                            Andy314A Offline
                            Andy314
                            wrote on last edited by
                            #15

                            Hallo, I think to understand the binary docx structrue is a very hard stuff.
                            Much more easy it would be to use the AcxtiveX interface for word.
                            Code something like this:

                            Open Doc1, select all text, copy to clipboard, close Doc1
                            open Doc2, set cursor to the end of the document, paste text, save doc2.

                            The AciteveX interface of Qt is very good and I have much experiences with it. I use it for Excel (fill Tables) and for Access (open Report an export to PDF.

                            You can hide the office windows by this task, so that the user sees nothing of you tasks.

                            D 1 Reply Last reply
                            1
                            • D Dante Pham

                              @mrjj After doing some research I think DocX may just work. However, I have never had any exprience dealing with .dll files, let alone using it in Qt. Do you have any suggestions on how to use it ?

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #16

                              @Dante-Pham
                              Hi.
                              Using a DLL involves loading it and resolve symbols.
                              Meaning to be able to run functions from it.
                              You also need a .h file. (didnt see any?)
                              You should also get a copy of visual studio as .NET is best/easy/to used from that IDE.
                              Then you shall google calling a .net DLL from C++ as there might be other things to look out for.

                              Maybe @VRonin knows a easier way to use the .DLL in Qt.

                              Update:
                              Its available via Nuget
                              so it should be easy to install and try the samples directly in VS.
                              Then try to mix with Qt.

                              Update 2:
                              Using VBA, this short code can merge all docs from a folder.

                              Sub MergeDocs()
                                  Dim rng As Range
                                  Dim MainDoc As Document
                                  Dim strFile As String, strFolder As String
                                  Dim Count As Long
                                  With Application.FileDialog(msoFileDialogFolderPicker)
                                      .Title = "Pick folder"
                                      .AllowMultiSelect = False
                                      If .Show Then
                                          strFolder = .SelectedItems(1) & Application.PathSeparator
                                      Else
                                          Exit Sub
                                      End If
                                  End With
                                  Set MainDoc = Documents.Add
                                  strFile = Dir$(strFolder & "*.docx") ' can change to .doc
                                  Count = 0
                                  Do Until strFile = ""
                                      Count = Count + 1
                                      Set rng = MainDoc.Range
                                      With rng
                                          .Collapse 0
                                          If Count > 1 Then
                                              .InsertBreak 2
                                              .End = MainDoc.Range.End
                                              .Collapse 0
                                          End If
                                          .InsertFile strFolder & strFile
                                      End With
                                      strFile = Dir$()
                                  Loop
                                  MsgBox ("Files are merged")
                              lbl_Exit:
                                  Exit Sub
                              End Sub
                              

                              Credits to gmaxey.
                              http://www.vbaexpress.com/forum/showthread.php?51797-Macro-to-merge-mulitple-word-doc-into-one-word-doc

                              D 1 Reply Last reply
                              1
                              • VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #17

                                For what you need to do I'd point you to http://stackoverflow.com/questions/18351829/merge-multiple-word-documents-into-one-open-xml/18352219#18352219

                                Using .Net dlls in visual studio is extremely easy, just open the project property and add a reference to the .dll, then right click on the .cpp file you need to use the library in, go into properties and activate /clr in the "Common Language RunTime Support" field under general. Now you can use C++/CLI in it which is quite close to C#

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                1 Reply Last reply
                                2
                                • Andy314A Andy314

                                  Hallo, I think to understand the binary docx structrue is a very hard stuff.
                                  Much more easy it would be to use the AcxtiveX interface for word.
                                  Code something like this:

                                  Open Doc1, select all text, copy to clipboard, close Doc1
                                  open Doc2, set cursor to the end of the document, paste text, save doc2.

                                  The AciteveX interface of Qt is very good and I have much experiences with it. I use it for Excel (fill Tables) and for Access (open Report an export to PDF.

                                  You can hide the office windows by this task, so that the user sees nothing of you tasks.

                                  D Offline
                                  D Offline
                                  Dante Pham
                                  wrote on last edited by
                                  #18

                                  @Andy314 I am using ActiveX but i have no idea what functions are used to do what you describe. Would you mind giving me some examples ? How do I copy and paste ? How to hide the window ?

                                  Andy314A 1 Reply Last reply
                                  0
                                  • mrjjM mrjj

                                    @Dante-Pham
                                    Hi.
                                    Using a DLL involves loading it and resolve symbols.
                                    Meaning to be able to run functions from it.
                                    You also need a .h file. (didnt see any?)
                                    You should also get a copy of visual studio as .NET is best/easy/to used from that IDE.
                                    Then you shall google calling a .net DLL from C++ as there might be other things to look out for.

                                    Maybe @VRonin knows a easier way to use the .DLL in Qt.

                                    Update:
                                    Its available via Nuget
                                    so it should be easy to install and try the samples directly in VS.
                                    Then try to mix with Qt.

                                    Update 2:
                                    Using VBA, this short code can merge all docs from a folder.

                                    Sub MergeDocs()
                                        Dim rng As Range
                                        Dim MainDoc As Document
                                        Dim strFile As String, strFolder As String
                                        Dim Count As Long
                                        With Application.FileDialog(msoFileDialogFolderPicker)
                                            .Title = "Pick folder"
                                            .AllowMultiSelect = False
                                            If .Show Then
                                                strFolder = .SelectedItems(1) & Application.PathSeparator
                                            Else
                                                Exit Sub
                                            End If
                                        End With
                                        Set MainDoc = Documents.Add
                                        strFile = Dir$(strFolder & "*.docx") ' can change to .doc
                                        Count = 0
                                        Do Until strFile = ""
                                            Count = Count + 1
                                            Set rng = MainDoc.Range
                                            With rng
                                                .Collapse 0
                                                If Count > 1 Then
                                                    .InsertBreak 2
                                                    .End = MainDoc.Range.End
                                                    .Collapse 0
                                                End If
                                                .InsertFile strFolder & strFile
                                            End With
                                            strFile = Dir$()
                                        Loop
                                        MsgBox ("Files are merged")
                                    lbl_Exit:
                                        Exit Sub
                                    End Sub
                                    

                                    Credits to gmaxey.
                                    http://www.vbaexpress.com/forum/showthread.php?51797-Macro-to-merge-mulitple-word-doc-into-one-word-doc

                                    D Offline
                                    D Offline
                                    Dante Pham
                                    wrote on last edited by
                                    #19

                                    @mrjj
                                    _ DocX only has a .DLL file and a documentation (.chm file) which is very unfinished. Lots of explainations in there are missing. After a while I found myself lost and my code messed up. I tried emailing the guy and he hasn't replied to me yet. Still, I did try adding library into Qt but it only takes .lib files. I found a documentation which show me how to add .dll files but it still didn't work after numerous tries
                                    _ I don't think Nuget and VBA are going to work for me because I don't know anything about them, which makes it difficult for me to modify and make tweaks to them ( which i am definitely going to ). Still, I have to do more research on them. I will be back very soon

                                    mrjjM 1 Reply Last reply
                                    1
                                    • D Dante Pham

                                      @mrjj
                                      _ DocX only has a .DLL file and a documentation (.chm file) which is very unfinished. Lots of explainations in there are missing. After a while I found myself lost and my code messed up. I tried emailing the guy and he hasn't replied to me yet. Still, I did try adding library into Qt but it only takes .lib files. I found a documentation which show me how to add .dll files but it still didn't work after numerous tries
                                      _ I don't think Nuget and VBA are going to work for me because I don't know anything about them, which makes it difficult for me to modify and make tweaks to them ( which i am definitely going to ). Still, I have to do more research on them. I will be back very soon

                                      mrjjM Offline
                                      mrjjM Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #20

                                      @Dante-Pham said in How to read and write docx files in Qt:

                                      Nuget

                                      That is just a system to easy get a lib for Visual Studio.
                                      Click and Play , so to speak :)

                                      1 Reply Last reply
                                      1
                                      • D Offline
                                        D Offline
                                        Dante Pham
                                        wrote on last edited by
                                        #21

                                        Ok so thanks to @VRonin , I found this https://youtu.be/qOfiiYP2JTI?t=2m11s , in which is pretty much what I'm looking for ! The only problem is I don't know C# . Is there any way we can do that in Qt in C++ ??

                                        mrjjM VRoninV 2 Replies Last reply
                                        0
                                        • D Dante Pham

                                          Ok so thanks to @VRonin , I found this https://youtu.be/qOfiiYP2JTI?t=2m11s , in which is pretty much what I'm looking for ! The only problem is I don't know C# . Is there any way we can do that in Qt in C++ ??

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #22

                                          @Dante-Pham
                                          Well if you know c++ then C# is pretty easy.
                                          https://msdn.microsoft.com/en-us/library/yyaad03b(v=vs.90).aspx

                                          I think you can do it almost same way with Visual Studio and c++ but of course the syntax will be
                                          different.

                                          D 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved