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
QtWS25 Last Chance

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 24 Oct 2018, 11:45 last edited by aha_1980
    #1

    Hi!
    How can I read doc and docx files?

    J 1 Reply Last reply 24 Oct 2018, 11:50
    0
    • M Mikeeeeee
      24 Oct 2018, 11:45

      Hi!
      How can I read doc and docx files?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 24 Oct 2018, 11:50 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 24 Oct 2018, 13:29 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 24 Oct 2018, 14:50
        0
        • M Mikeeeeee
          24 Oct 2018, 13:29

          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 24 Oct 2018, 14:50 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
          • V Offline
            V Offline
            VRonin
            wrote on 24 Oct 2018, 15:04 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 24 Oct 2018, 16:03 last edited by
              #6

              @VRonin said in Read doc and docx fils:

              QAxObject

              How can I use QAxObject?

              J 1 Reply Last reply 24 Oct 2018, 16:34
              0
              • V Offline
                V Offline
                VRonin
                wrote on 24 Oct 2018, 16:14 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
                  24 Oct 2018, 16:03

                  @VRonin said in Read doc and docx fils:

                  QAxObject

                  How can I use QAxObject?

                  J Offline
                  J Offline
                  JonB
                  wrote on 24 Oct 2018, 16:34 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 25 Oct 2018, 09:00 last edited by
                    #9

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

                    J 1 Reply Last reply 25 Oct 2018, 09:23
                    0
                    • M Mikeeeeee
                      25 Oct 2018, 09:00

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

                      J Offline
                      J Offline
                      JonB
                      wrote on 25 Oct 2018, 09:23 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 25 Oct 2018, 12:27 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;
                        
                        J 1 Reply Last reply 25 Oct 2018, 12:42
                        1
                        • M Mikeeeeee
                          25 Oct 2018, 12:27

                          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;
                          
                          J Offline
                          J Offline
                          JonB
                          wrote on 25 Oct 2018, 12:42 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
                          • J JonB referenced this topic on 28 May 2023, 16:05

                          4/12

                          24 Oct 2018, 14:50

                          topic:navigator.unread, 8
                          • Login

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