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. Read doc and docx files
Forum Update on Monday, May 27th 2025

Read doc and docx files

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 3.4k Views
  • 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by aha_1980
    #1

    Hi!
    How can I read doc and docx files?

    jsulmJ 1 Reply Last reply
    0
    • M Mikeeeeee

      Hi!
      How can I read doc and docx files?

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

      @Mikeeeeee https://forum.qt.io/topic/74254/how-to-read-and-write-docx-files-in-qt

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        In this code txt file is normaly read, but docx return "PK\u0003\u0004\u0014" , then text in file "Test Text for docx "

        #include "mainwindow.h"
        #include <QApplication>
        #include <QFile>  //для работы с файлами
        #include <QtConcurrent/QtConcurrent> //for qDebug
        
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
        
        
            QFile TrainingFile("C:\\ForResume\\2.docx");
            //QFile TrainingFile("C:\\ForResume\\1.txt");
            QString Text;
            if (TrainingFile.open(QIODevice::ReadOnly))
            {
                Text = TrainingFile.readAll();
            }
            qDebug()<<TrainingFile;
            qDebug()<<Text;
        
        
            w.show();
        
            return a.exec();
        }
        
        
        M 1 Reply Last reply
        0
        • M Mikeeeeee

          In this code txt file is normaly read, but docx return "PK\u0003\u0004\u0014" , then text in file "Test Text for docx "

          #include "mainwindow.h"
          #include <QApplication>
          #include <QFile>  //для работы с файлами
          #include <QtConcurrent/QtConcurrent> //for qDebug
          
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MainWindow w;
          
          
              QFile TrainingFile("C:\\ForResume\\2.docx");
              //QFile TrainingFile("C:\\ForResume\\1.txt");
              QString Text;
              if (TrainingFile.open(QIODevice::ReadOnly))
              {
                  Text = TrainingFile.readAll();
              }
              qDebug()<<TrainingFile;
              qDebug()<<Text;
          
          
              w.show();
          
              return a.exec();
          }
          
          
          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @Mikeeeeee said in Read doc and docx fils:

          In this code txt file is normaly read, but docx return "PK\u0003\u0004\u0014"

          PK is the signature of a zip file.
          Seems pretty normal since docx files are zip files ;)

          1 Reply Last reply
          4
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            It's not that trivial.
            The documentation that specifies the MS Excel formats is ISO/IEC 29500-# (# being 1, 2, 3 and 4) which is ~7000 pages of standard.
            A library that handles that format is a monumental project.

            Your best bet is touse QAxObject to interact with MS word and use its engine to read/write the file

            "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
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #6

              @VRonin said in Read doc and docx fils:

              QAxObject

              How can I use QAxObject?

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

                Summoning the help of the expert @hskoglund

                "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
                0
                • M Mikeeeeee

                  @VRonin said in Read doc and docx fils:

                  QAxObject

                  How can I use QAxObject?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Mikeeeeee said in Read doc and docx fils:

                  @VRonin said in Read doc and docx fils:

                  QAxObject

                  How can I use QAxObject?

                  Did you start by looking at post https://forum.qt.io/topic/74254/how-to-read-and-write-docx-files-in-qt/26 in @jsulm's link?

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    Mikeeeeee
                    wrote on last edited by
                    #9

                    I do not understand how in this example to get the text from the file.

                    JonBJ 1 Reply Last reply
                    0
                    • M Mikeeeeee

                      I do not understand how in this example to get the text from the file.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @Mikeeeeee
                      It's an example in response to your

                      How can I use QAxObject?

                      illustrating how you can use that to talk to VBA in Word. Which is what you asked for. It does not show how to retrieve the document contents. That is for you to look up, to discover whatever the necessary VBA is to do that. I am not going to do that for you. I cannot say whether someone else is prepared to do the work and write the code for you.

                      1 Reply Last reply
                      2
                      • M Offline
                        M Offline
                        Mikeeeeee
                        wrote on last edited by
                        #11

                        M ore information here
                        This code works

                        QAxObject   wordApplication("Word.Application");
                            QAxObject *documents = wordApplication.querySubObject("Documents");
                            QAxObject *document = documents->querySubObject("Open(const QString&, bool)", "C:\\ForResume\\2.docx", true);
                            QAxObject *words = document->querySubObject("Words");
                            QString textResult;
                            int countWord = words->dynamicCall("Count()").toInt();
                            for (int a = 1; a <= countWord; a++){
                            textResult.append(words->querySubObject("Item(int)", a)->dynamicCall("Text()").toString());
                            }
                            qDebug()<<textResult;
                        
                        JonBJ 1 Reply Last reply
                        1
                        • M Mikeeeeee

                          M ore information here
                          This code works

                          QAxObject   wordApplication("Word.Application");
                              QAxObject *documents = wordApplication.querySubObject("Documents");
                              QAxObject *document = documents->querySubObject("Open(const QString&, bool)", "C:\\ForResume\\2.docx", true);
                              QAxObject *words = document->querySubObject("Words");
                              QString textResult;
                              int countWord = words->dynamicCall("Count()").toInt();
                              for (int a = 1; a <= countWord; a++){
                              textResult.append(words->querySubObject("Item(int)", a)->dynamicCall("Text()").toString());
                              }
                              qDebug()<<textResult;
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @Mikeeeeee
                          That's fine. Are you actually wanting to get the text word-by-word as you have shown? Just saying: it must be horrendously inefficient compared to getting the whole text, but that's OK if that's how you intend.

                          1 Reply Last reply
                          0
                          • JonBJ JonB referenced this topic on

                          • Login

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