我目前正在尝试修改Windows Word资源管理器中MS Word .docx文件属性(即,从Windows资源管理器>属性>详细信息右键单击文件)中“详细信息”部分中显示的“内容状态”字段,如下所示下面的屏幕截图。
有谁知道如何使用Perl脚本(或最终批处理脚本或python)更改“内容状态”的值?
我熟悉Win32:OLE来修改Excel和Word文档,但不知道如何解决该问题。
谢谢
感谢“ Haf Linger”,我设法找到了解决问题的答案:
#!perl
use strict;
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
##CONFIG##
my $new_status="Closed";
my $docx_name="TEST.docx";
my $tmp_name="tmp.docx";
my $replace_file=1; #set to 0 for testing purpose
####
#Open docx file
my $doc = Archive::Zip->new($docx_name);
#Retrieve old status
my $old_status=undef;
my $new_content=$doc->contents('docProps/core.xml');
if ($new_content=~s%<cp:contentStatus>\s*(.*?)\s*</cp:contentStatus>%%) {
$old_status=$1;
} else {}
#Update with new status
printf "Update status of $docx_name to '$new_status'%s: ",
(defined $old_status ? " (previously '$old_status')" : "");
$new_content=~s%(</cp:coreProperties>)%<cp:contentStatus>$new_status</cp:contentStatus>$1%;
my ($content, $status) = $doc->contents('docProps/core.xml',$new_content);
if ($status) {
print "error during update of status of $docx_name ($!)\n";
} else {
print "done!\n";
}
#Save file to temporary file
unless ( $doc->writeToFileNamed($tmp_name) == AZ_OK ) {
die 'write error';
}
#Replace if needed
if ($replace_file) {
use File::Copy;
move($tmp_name, $docx_name);
} else {}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句