不用WordDocument如何把word文档转换成TXT?(100分)

  • 主题发起人 heipi2003
  • 开始时间
H

heipi2003

Unregistered / Unconfirmed
GUEST, unregistred user!
以前好像也看过人家这么做,没有另开word文档就实现了把word里面的内容
逐行取出来,速度也相当快,当时也不懂
现在我也面对了同样的问题,希望速度要快(文档全部是纯文字的),最好
不要另外存成文本,因为文档很多,文档格式也相同
都是如下格式:
[文件]abc.doc
[作者]xxx
[说明]xxx
......
只要能取出文本,分析的活就不用您操心了,只有100分了:(
 
没人理我?
 
没开word,可能是用的com形式的word,读出文件,输出为txt.
看看delphi中关于word方面的com的调用
 
WordApplication1: TWordApplication;
WordDocument1: TWordDocument;

procedure TForm1.Button2Click(Sender: TObject);
var
ItemIndex: OleVariant;
FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format, SaveChanges: OleVariant;
begin

try
Wordapplication1.Connect;
except
on E: Exception do
begin
Showmessage(E.Message);
Abort;
end;
end;
Wordapplication1.Visible := false;
WordApplication1.Caption := 'Delphi automation';

FileName := 'c:/新建 Microsoft Word 文档.doc'; //要写Full Path Name;
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;

WordApplication1.Documents.Open(FileName, ConfirmConversions, ReadOnly,
AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument,
WritePasswordTemplate, Format);

{Assign WordDocument component}
ItemIndex := 1;
WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));

Format := wdFormatText;//wdFormatRTF;
FileName := 'c:/newrtf.txt';
WordDocument1.SaveAs(FileName,Format);
//WordDocument1.Disconnect;
SaveChanges := wdDoNotSaveChanges;
WordApplication1.Quit(SaveChanges);
WordApplication1.Disconnect;

end;
 
同意楼上的。
 
谢谢zw84611,这种方法我也能想到,不过还没有写成代码,辛苦你了
用visible:=false也不知道不开word的情况下能不能通过!!
不过我估计速度不会太快(可能会很慢):(
放心我会给高分的:)

一生中最爱, 能不能讲详细一点,没有接触过呀!

如果还有办法请跟贴
 
可以通过,我试过。不过速度就不知道了,我试的文件很小。
 
读每一段(行):
以下不会显示 word 窗口的。
procedure TForm1.Button2Click(Sender: TObject);
var
wordapp,doc:eek:levariant;
i:integer;
begin
wordapp:=createoleobject('Word.application');
try
doc:=wordapp.Documents.Open(FileName:='c:/my documents/网络通信.doc');
Memo1.clear;
for i:=1 to doc.Paragraphs.count do
memo1.lines.add(doc.Paragraphs.item(i).range.text);
memo1.Lines.SaveToFile('d:/d.txt');
finally
wordapp.quit;
end;
 
Word的格式是安装微软的存储结构的方式存储的!以上的代码都是要安装Word,才能分析的!
下面的是真真不需要Word的!!!!
unit FindText;

interface

function FindTextInFile(const FileName, TextToFind: WideString): boolean;

implementation

uses ComObj, ActiveX, AxCtrls, SysUtils, Classes;

function FindTextInFile(const FileName, TextToFind: WideString): boolean;
var Root: IStorage;
EnumStat: IEnumStatStg;
Stat: TStatStg;
iStm: IStream;
Stream: TOleStream;
DocTextString: WideString;
begin
Result:=False;

if not FileExists(FileName) then Exit;

// Check to see if it's a structured storage file
if StgIsStorageFile(PWideChar(FileName)) <> S_OK then Exit;

// Open the file
OleCheck(StgOpenStorage(PWideChar(FileName), nil,
STGM_READ or STGM_SHARE_EXCLUSIVE, nil, 0, Root));

// Enumerate the storage and stream objects contained within this file
OleCheck(Root.EnumElements(0, nil, 0, EnumStat));

// Check all objects in the storage
while EnumStat.Next(1, Stat, nil) = S_OK do

// Is it a stream with Word data
if Stat.pwcsName = 'WordDocument' then

// Try to get the stream "WordDocument"
if Succeeded(Root.OpenStream(Stat.pwcsName, nil,
STGM_READ or STGM_SHARE_EXCLUSIVE, 0, iStm)) then
begin
Stream:=TOleStream.Create(iStm);
try
if Stream.Size > 0 then
begin
// Move text data to string variable
SetLength(DocTextString, Stream.Size);
Stream.Position:=0;
Stream.Read(pChar(DocTextString)^, Stream.Size);

// Find a necessary text
Result:=(Pos(TextToFind, DocTextString) > 0);
end;
finally
Stream.Free;
end;
Exit;
end;
end;

end.
 
为什么不用TFileStream呢
var
F:TFileStream;
Size:int64;
NumRead:integer;
begin
F:=TfileStream.Create('test.doc',fmOpenRead);
Size:=F.size;
NumRead:= F.Read(Buf, SizeOf(Buf));
end;
 
谢谢各位,我正在逐一试验!
 
jsxjd的代码很实际,决定用它了
快乐,你的代码试验过了,最符合条件,肯定会用的上(一看就知道,这么高深:)
zw84611的代码老了点,不过还算好用:)
magiclynn,我对stream不是特别熟
多谢各位,分数不多还请笑纳

 
多人接受答案了。
 
Format := wdFormatText;
还有什么样的Format可以插入换行符的
 
顶部