在Git中,如何对比Microsoft Word文档?

10
我一直在按照这里的指南来比较微软Word文档,但是我遇到了这个错误:
Usage:  /usr/bin/docx2txt.pl [infile.docx|-|-h] [outfile.txt|-]
        /usr/bin/docx2txt.pl < infile.docx
        /usr/bin/docx2txt.pl < infile.docx > outfile.txt

        In second usage, output is dumped on STDOUT.

        Use '-h' as the first argument to get this usage information.

        Use '-' as the infile name to read the docx file from STDIN.

        Use '-' as the outfile name to dump the text on STDOUT.
        Output is saved in infile.txt if second argument is omitted.

Note:   infile.docx can also be a directory name holding the unzipped content
        of concerned .docx file.

fatal: unable to read files to diff

解释一下我是如何犯这个错误的:我在我想要进行差异比较的代码库中创建了一个.gitattributes文件。.gitattributes文件的内容如下:
*.docx diff=word
*.docx difftool=word

我已经安装了docx2txt。我使用的是Linux系统。我创建了一个名为docx2txt的文件,其中包含以下内容:
#!/bin/bash
docx2txt.pl $1 -

我使用命令$ chmod a+x给docx2txt添加了可执行权限,并将docx2txt放在了/usr/bin/目录下。
我执行了以下操作:
$ git config diff.word.textconv docx2txt

然后我尝试对比两个Microsoft Word文档。就是在那时我遇到了上面提到的错误。
我错过了什么?我该如何解决这个错误?
附注:我不知道我的shell是否能找到docx2txt,因为当我执行这个操作时:
$ docx2txt

我的终端卡住了,正在处理某些东西,但没有输出任何内容,当我执行这些命令时就会发生这种情况:
$ man docx2txt
No manual entry for docx2txt
$ docx2txt --help
Can't read docx file <--help>!

进展更新:我将docx2txt更改为
#!/bin/bash
docx2txt.pl "$1" -

正如pmod建议的那样,现在可以通过命令行使用git diff <commit>命令了!太棒了!
不过,当我尝试时,
$ git difftool <commit>

Git启动kdiff3时,我遇到了这个弹出错误:
Some input characters could not be converted to valid unicode.
You might be using the wrong codec. (e.g. UTF-8 for non UTF-8 files).
Don't save the result if unsure. Continue at your own risk.
Affected input files are in A, B.

...而且文件中的所有字符都是胡言乱语。命令行正确显示了差异文本,但是由于某种原因,kdiff3无法正确显示差异文本。
我应该如何在kdiff3或其他GUI工具中正确显示差异文本?我应该将kdiff3更换为其他工具吗?
额外信息:我的shell似乎无法找到docx2txt,可能是因为这些命令:
$ which doctxt
which: no doctxt in (/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl)

$ which docx2txt
/usr/bin/docx2txt
2个回答

4

4

doc2txt.pl根据用法要求需要精确地传入两个参数或零个参数。在您的情况下,第一个参数是文件名或“-”。因此,您的包装脚本看起来正确,但当作为第一个参数传递的文件名中至少有一个空格时就会出现问题。在这种情况下,在$1扩展后,文件名部分将作为单独的参数传递,因此工具输出用法信息,因为它读取了超过2个参数。

尝试使用引号以避免文件名分割:

#!/bin/bash
docx2txt.pl "$1" -

PS:我不知道我的shell是否能找到docx2txt

您可以使用以下命令进行检查:

$ which docx2txt

如果你看到了路径,那么工具(二进制文件或可执行脚本)就可以被找到(基于PATH环境变量)。
因为当我执行以下命令时: $ docx2txt 我的终端会冻结,处理一些东西,但没有输出任何内容。
如果没有参数,你的脚本将执行doc2txt.pl -,根据工具的用法,它期望通过STDIN传递输入文件,也就是你正在输入的内容。因此,它看起来像是在等待并处理某些东西,但实际上只是捕获了你的输入。

我按照你的建议更改了docx2txt,现在git diff可以工作了!非常感谢你的提示,它确实帮了我大忙。但是,git difftool <comment>会抛出一个错误,我在更新中解释了这个错误,即输入字符未转换为有效的Unicode。不太理解。有什么想法如何解决吗? - Jack
@Jack,感谢您的接受,请使用"$ which docx2txt" - 这是我回答中的一个打字错误。关于新问题/更新 - 请创建一个新的问题,因为SE基于问题->答案的原则工作,这样其他人就可以更容易地找到解决方案。 - pmod
@Jack 好的,我在你另一个问题中发布了答案,至少它的标题中包含了kdiff3和“可读字符” - 这样更接近这个问题的主题。 - pmod

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接