Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
2015年使用Perl实现OLE自动化_Perl_Ole - Fatal编程技术网

2015年使用Perl实现OLE自动化

2015年使用Perl实现OLE自动化,perl,ole,Perl,Ole,我试图从Perl中打开Word docx文件,然后将其另存为HTML。我得到了Win7 63位和Office 15(Office 365订阅)。我读了一些例子,并尝试了和,但我总是得到错误 perl -MWin32::OLE -e "$wd = Win32::OLE->GetObject('1994.62_01_fnd_en.docx'); print Win32::OLE->LastError" Win32::OLE(0.1712) error 0x80004005: "Unspe

我试图从Perl中打开Word docx文件,然后将其另存为HTML。我得到了Win7 63位和Office 15(Office 365订阅)。我读了一些例子,并尝试了和,但我总是得到错误

perl -MWin32::OLE -e "$wd = Win32::OLE->GetObject('1994.62_01_fnd_en.docx'); print Win32::OLE->LastError"
Win32::OLE(0.1712) error 0x80004005: "Unspecified error"

perl -e "use Win32::OLE::Const('.*Word.*')"
No type library matching ".*Word.*" found at -e line 1.
Win32::OLE(0.1712): GetOleTypeLibObject() Not a Win32::OLE::TypeLib object at C:/Perl64/lib/Win32/OLE/Const.pm line 49.

虽然GetObject不适合我,但Win32::OLE->new('Word.Application')工作正常,我可以用这样的脚本来完成这项工作

use Win32::OLE; # http://search.cpan.org/~jdb/Win32-OLE-0.1712/lib/Win32/OLE.pm
use Win32::OLE::Variant; # http://search.cpan.org/~jdb/Win32-OLE-0.1712/lib/Win32/OLE/Variant.pm
use constant true  => Variant->new(VT_BOOL, 'true');
use constant false => Variant->new(VT_BOOL, 'false');

use Cwd;
# use Path::Abstract qw(path); # http://search.cpan.org/~rokr/Path-Abstract-0.096/lib/Path/Abstract.pm#$path->extension
use constant MAX => 1024000; # max file size to open

# https://msdn.microsoft.com/en-us/library/office/ff839952.aspx
use constant wdFormatUnicodeText => 7;
use constant wdFormatFilteredHTML => 10;
# use Win32::OLE::Const '.*Microsoft Word'; # http://search.cpan.org/~jdb/Win32-OLE-0.1712/lib/Win32/OLE/Const.pm
# No type library matching ".*Word" found at -e line 1.
# Win32::OLE(0.1712): GetOleTypeLibObject() Not a Win32::OLE::TypeLib object at C:/Perl64/lib/Win32/OLE/Const.pm line 49.

my $w = Win32::OLE->new('Word.Application');
# https://msdn.microsoft.com/en-us/library/aa171814(v=office.11).aspx
$w->ChangeFileOpenDirectory(cwd);
for my $doc (<doc/*>) {
  next if -s $doc > MAX;
  my $html = $doc; $html =~ s{\bdocx?\b}{html}g;
  my $txt  = $doc; $txt  =~ s{\bdocx?\b}{txt}g;
  # https://msdn.microsoft.com/EN-US/library/office/ff835182.aspx
  $d = $w->Documents->Open ($doc, {ConfirmConversions => false, ReadOnly => true, OpenAndRepair => false, AddToRecentFiles => false, Visible => false});
  # https://msdn.microsoft.com/en-us/library/office/ff836084.aspx
  $d->SaveAs2({FileName => $html, FileFormat => wdFormatFilteredHTML});
  $d->SaveAs2({FileName => $txt,  FileFormat => wdFormatUnicodeText});
  # https://msdn.microsoft.com/EN-US/library/office/ff196343.aspx
  $d->Close;
  last;
}
使用Win32::OLE;#http://search.cpan.org/~jdb/Win32-OLE-0.1712/lib/Win32/OLE.pm
使用Win32::OLE::Variant;#http://search.cpan.org/~jdb/Win32-OLE-0.1712/lib/Win32/OLE/Variant.pm
使用常量true=>Variant->new(VT_BOOL,'true');
使用常量false=>Variant->new(VT_BOOL,'false');
使用化学武器;
#使用路径::抽象qw(路径);#http://search.cpan.org/~rokr/Path-Abstract-0.096/lib/Path/Abstract.pm#$Path->extension
使用常量MAX=>1024000;#要打开的最大文件大小
# https://msdn.microsoft.com/en-us/library/office/ff839952.aspx
使用常量wdFormatUnicodeText=>7;
使用常量wdFormatFilteredHTML=>10;
#使用Win32::OLE::Const'.*Microsoft Word'.#http://search.cpan.org/~jdb/Win32-OLE-0.1712/lib/Win32/OLE/Const.pm
#在-e第1行找不到与“*Word”匹配的类型库。
#Win32::OLE(0.1712):GetOleTypeLibObject()不是位于C:/Perl64/lib/Win32/OLE/Const.pm第49行的Win32::OLE::TypeLib对象。
我的$w=Win32::OLE->new('Word.Application');
# https://msdn.microsoft.com/en-us/library/aa171814(v=office.11)
$w->ChangeFileOpenDirectory(cwd);
我的$doc(){
下一个if-s$doc>MAX;
my$html=$doc;$html=~s{\bdocx?\b}{html}g;
我的$txt=$doc;$txt=~s{\bdocx?\b}{txt}g;
# https://msdn.microsoft.com/EN-US/library/office/ff835182.aspx
$d=$w->Documents->Open($doc,{ConfirmConversions=>false,ReadOnly=>true,OpenAndRepair=>false,AddToRecentFiles=>false,Visible=>false});
# https://msdn.microsoft.com/en-us/library/office/ff836084.aspx
$d->SaveAs2({FileName=>html,FileFormat=>wdFormatFilteredHTML});
$d->SaveAs2({FileName=>txt,FileFormat=>wdformatUnicodext});
# https://msdn.microsoft.com/EN-US/library/office/ff196343.aspx
$d->Close;
最后;
}
在Chrome中显示的是空帧,只是底部的一些脚本源