更新详细信息"内容状态" Perl / Batch / Python的MS Word文档

时间:2015-06-05 04:22:40

标签: python perl batch-file ms-word

我目前正在尝试修改字段"内容状态"出现在Windows资源管理器中的"详细信息" MS Word .docx文件属性的一部分(即从Windows资源管理器右键单击文件> 属性> 详细信息),如图所示下面的截图。

Right Click > Properties > Details

有谁知道如何更改"内容状态"使用Perl脚本(或最终批处理脚本或python)?

我熟悉Win32:OLE来修改Excel和Word文档,但不知道如何解决问题。

由于

2 个答案:

答案 0 :(得分:1)

属性<cp:contentStatus>可以在docProps/core.xml文件中找到。 更改此xml元素的内容将更新您引用的字段。 (假设你在这里谈论docx文件)

使用Archive::Zip,您将能够打开docx文件并访问core.xml文件并更改其内容并将其压缩回更新的docx文件。

答案 1 :(得分:0)

感谢Haf Linger&#39;,我设法找到了问题的答案:

#!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 {}