接受的解决方案(使用字符串/解压缩)在 Linux Mint 19.3 上对我来说效果不佳。对于大多数 doc/docx/rtf/xls 文件以及它们的 LibreOffice 对应文件,以下似乎工作得很好。其中一些可能通过 cygwin/git bash 在 Windows 上运行,但我没有测试过;如果我提到的软件包在 cygwin/git bash 中不可用,那么我会寻找执行相同转换的 python/perl 脚本并用它们代替。
- 安装先决条件:
sudo apt install git pandoc catdoc odt2txt。
- 请注意,catdoc 和 odt2txt 包含多个用于处理 doc/xls/ppt/odt/ods/odp 格式的工具,而不仅仅是包名称中的那些。同样,pandoc 处理所有较新的压缩“x”格式。
- 我希望我的属性应用为Global (e.g. User-scoped),而不是像其他答案中那样按项目应用。要创建用户范围的 git 属性文件,请使用
mkdir ~/.config/git/ && touch ~/.config/git/attributes(在 Windows 上应该是 mkdir "%USERPROFILE%\.config\git" && echo "" > "%USERPROFILE%\.config\git\attributes")
- 设置 git 属性文件(根据需要设置上一步中提到的用户范围文件或项目范围文件
${projectDir}/.git/info/attributes):
# handle windows *.reg files (utf-16 which git doesn't normally like)
*.reg diff=utf16
# handle misc common document formats
*.pdf diff=pdf
*.rtf diff=catdoc
# handle libre/open document formats
*.ods diff=ods2txt
*.odp diff=odp2txt
*.odt diff=odt2txt
# handle older common ms document formats
# note: ppt did not work for me
*.doc diff=catdoc
*.ppt diff=catppt
*.xls diff=xls2csv
# handle newer zipped ms document formats
# note: pptx and xlsx did not work for me
*.docx diff=pandoc
*.pptx diff=pandoc
*.xlsx diff=pandoc
- 创建 .gitconfig 定义(在用户范围的
~/.gitconfig 或项目范围的${projectDir}/.git/config 中)。其中大部分基于this article,但根据我自己的测试进行了更改。
[core]
autocrlf = false
[diff]
guitool = kdiff3
[diff "odp2txt"]
textconv = odp2txt
binary = true
[diff "odt2txt"]
textconv = odt2txt
binary = true
[diff "ods2txt"]
textconv = ods2txt
binary = true
[diff "catdoc"]
textconv = catdoc
binary = true
# note catppt did not work for me
[diff "catppt"]
textconv = catppt
binary = true
[diff "xls2csv"]
textconv = xls2csv
binary = true
[diff "xlsx2csv"]
textconv = xlsx2csv
binary = true
[diff "pandoc"]
textconv=pandoc --to=markdown
prompt = false
[diff "pdf2txt"]
textconv=pdf2txt
binary = true
[diff "utf16"]
textconv = iconv -c -f UTF-16LE -t ASCII
即使在从their github page 下载了最新版本的 pandoc 后,我也无法成功获得适用于 xlsx、ppt 或 pptx 的差异。即使使用 Mint/Ubuntu/Debian 存储库中的超级旧版本(2016 年的 v1.19.2.4),docx 转换也能正常工作。对于我使用的 xlsx/pptx 示例,我总是得到“无效的 UTF-8 流致命”(旧版本)或“UTF-8 解码错误”(新版本)。
这可能是由于我使用的示例文件(一些来自网络的示例和我通过转换 LibreOffice 文档创建的一些示例)、我的系统设置、我使用的版本或其他原因。
为了完整起见,在安装较新的 pandoc 后,我使用的是:
$ uname -vipor
5.3.0-40-generic #32~18.04.1-Ubuntu SMP Mon Feb 3 14:05:59 UTC 2020 x86_64 x86_64 GNU/Linux
$ dpkg -l catdoc odt2txt pandoc git xlsx2csv|grep '^ii'
ii catdoc 1:0.95-4.1 amd64 text extractor for MS-Office files
ii git 1:2.17.1-1ubuntu0.5 amd64 fast, scalable, distributed revision control system
ii odt2txt 0.5-1build2 amd64 simple converter from OpenDocument Text to plain text
ii pandoc 2.9.2-1 amd64 general markup converter
ii xlsx2csv 0.20+20161027+git5785081-1 all convert xslx files to csv format
编辑:还尝试使用包 xlsx2csv 进行 xlsx 转换而不是 pandoc,我也遇到了问题。可能与我的样本有关,但由于我并没有真正做任何特别的事情来创建它们,我会认为 xlsx2csv/pandoc 的覆盖率差距/限制如果是这样。