PDA

View Full Version : How to read .doc file and replace string in that file using Qt in Linux?



Meena Solanki
13th August 2015, 06:49
Hi Guys,

Is there any way to read a .doc file and replace string in that file using Qt in Linux?
Here is my code, I have tried:

{
QFile file("/home/.../Desktop/file.doc");
file.open(QFile::ReadOnly);
QTextStream in(&file);
QString data = in.readAll();
file.close();

QTextDocument *doc=new QTextDocument(data);
int j=data.indexOf("Current",0);
data.remove(j,7);
data.insert(j,"Present");

QTextDocumentWriter *writer=new QTextDocumentWriter("/home/.../Desktop/newfile.doc");
writer->setFormat(".doc");
writer->write(doc);
}

Please help me to solve this problem.

Thanks.

anda_skoa
13th August 2015, 09:38
If by "doc" you mean a Microsoft Word file, then you can't just treat is as simple plain text.

You need to look for a library that has import and export capability for that format.

Cheers,
_

Meena Solanki
19th August 2015, 15:19
Thanks for reply,

I know about the module "ActiveQt" which will allow me to read .doc file via qactivexcontroller.
But,I don't know, how to add this module in qt5.4.0 in linux.

Have you any idea??

anda_skoa
19th August 2015, 15:55
ActiveQt is a Qt wrapper of ActiveX, a Windows-only technology.

Cheers,
_

Meena Solanki
21st August 2015, 14:50
Hello,

Is there any other way to achieve this task??

anda_skoa
21st August 2015, 17:18
As I wrote before, you could look for a library that has I/O capabilities for the specific format (different versions of Word have different file formats).

Or find a tool that can read/write it and be scripted, e.g. LibreOffice Writer.

Cheers,
_

Meena Solanki
24th August 2015, 06:25
Ok Thanks.