【问题标题】:View .docx file on Github and use git diff on .docx file format在 Github 上查看 .docx 文件并在 .docx 文件格式上使用 git diff
【发布时间】:2014-03-16 16:28:01
【问题描述】:

我有两个问题:

  1. 有什么方法可以查看Github 上的.docx 文件?我们已将所有作业上传到 Github,但我们无法在浏览器中查看。如果我们不用下载文件就可以在浏览器中查看那些.docx文件就好了。

  2. 如何在.docx 文件格式上使用git diff?我尝试使用catdoc,但它对我不起作用。我想我以前在 Windows 上使用过 git diff .doc 格式,但它在 Mac 上不适合我。

非常感谢。

【问题讨论】:

  • catdoc 仅适用于 .doc 文件。
  • Word Diff 也可以在这里提供帮助

标签: git github


【解决方案1】:
  1. 回答问题的第二部分。 已经是一个旧帖子,但在没有答案的情况下突然出现在前 10 名。 通过以下设置,您可以在 docx 文件上获得一个穷人的差异。

在 .gitattributes 中使用:

*.docx diff=zip

在 .git/config 中使用:

[diff "zip"]
      textconv = unzip -c -a

作为奖励,我对旧词/excel 和新词/excel 的设置:

在 .gitattributes 中使用:

*.doc diff=word
*.xsl diff=excel
*.xlsx diff=zip
*.docx diff=zip

在 .git/config 中使用:

[diff "word"] 
    textconv = strings
[diff "excel"]
    textconv = strings
[diff "zip"]
    textconv = unzip -c -a

【讨论】:

    【解决方案2】:

    回答你的第二个问题 -

    通常当你尝试时

    git diff filename.docx
    

    你会得到表单的输出 -

    二进制文件 a/filename.docx 和 b/filename.docx 不同

    不是很有帮助。一个完美的解决方法是使用Pandoc

    • 通过以上链接在您的系统上安装 Pandoc。
    • 创建或编辑文件 ~/.gitconfig (linux, Mac) 或 "c:\Documents and Settings\user.gitconfig" (Windows) 添加(或使用git config --global --edit

      [diff "pandoc"]
           textconv=pandoc --to=markdown
           prompt = false
      [alias]
           wdiff = diff --word-diff=color --unified=1`
      
    • 在包含 .docx 文件的 git 控制目录中,创建或编辑文件 .gitattributes(linux、Windows 和 Mac)以添加

      *.docx diff=pandoc
      
    • 您可以提交 .gitattributes 以便在其他计算机上继续使用,但您需要在每台要使用的新计算机上编辑 ~/.gitconfig。 p>

    • 现在您可以看到自上次提交以来您对 .docx 文件所做的更改的彩色差异

       git wdiff file.docx
      

    更多详情请见here

    【讨论】:

      【解决方案3】:

      接受的解决方案(使用字符串/解压缩)在 Linux Mint 19.3 上对我来说效果不佳。对于大多数 doc/docx/rtf/xls 文件以及它们的 LibreOffice 对应文件,以下似乎工作得很好。其中一些可能通过 cygwin/git bash 在 Windows 上运行,但我没有测试过;如果我提到的软件包在 cygwin/git bash 中不可用,那么我会寻找执行相同转换的 python/perl 脚本并用它们代替。

      1. 安装先决条件:sudo apt install git pandoc catdoc odt2txt
      2. 请注意,catdoc 和 odt2txt 包含多个用于处理 doc/xls/ppt/odt/ods/odp 格式的工具,而不仅仅是包名称中的那些。同样,pandoc 处理所有较新的压缩“x”格式。
      3. 我希望我的属性应用为Global (e.g. User-scoped),而不是像其他答案中那样按项目应用。要创建用户范围的 git 属性文件,请使用 mkdir ~/.config/git/ && touch ~/.config/git/attributes(在 Windows 上应该是 mkdir "%USERPROFILE%\.config\git" && echo "" > "%USERPROFILE%\.config\git\attributes"
      4. 设置 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
      
      1. 创建 .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 的覆盖率差距/限制如果是这样。

      【讨论】:

        【解决方案4】:

        有没有办法在 Github 上查看 .docx 文件?

        除非 Word 文档是纯文本,否则还没有(2016 年第四季度)。

        如何在 .docx 文件格式上使用 git diff?

        由于 git for Windows 1.9.5 和 Git for Windows 2.5.3(2015 年 9 月和 issue 355),您无需进行任何自定义设置:

        git diff -- myWord.docx
        

        这会奏效。 (它也适用于.doc.pdf

        由于Git for Windows 2.10.1,您也可以区分.docm.dotm(参见PR 128)。

        【讨论】:

          【解决方案5】:

          半心半意地在 Stackoverflow 和 Google 上转了几年,今天才发现官方的 git 书有一个walkthrough

          1. 安装docx2txt。在 Ubuntu 16.04 上,我只使用了官方存储库:

            sudo apt-get install docx2txt
            
          2. 编写一个包装脚本(docx2txt 需要一些参数。)如下:

            #! /usr/bin/env bash
            docx2txt "$1" -
            
          3. 我将脚本称为d2t,所以我将它添加到我的$PATH 的某个文件夹中。请记住使其可执行,以便 git 可以运行它。

            chmod +x d2t
            mv d2t /somewhere/in/your/PATH
            
          4. 现在通过将此块添加到 .git/config 来让您的存储库意识到这一点:

            [diff "word"]
                textconv = d2t
            

            *注意:book 建议使用一个命令,我假设您也可以将其与--global 标志一起使用,以便将此过滤器应用于所有存储库(如果您愿意):

            git config --global diff.word.textconv d2t
            
          5. 对于您希望它工作的存储库,编辑.gitattributes

            *.docx diff=word
            
          6. 现在您应该可以git diff 您的 docx 文档了。

            diff --git a/goodpoint.docx b/goodpoint.docx
            index 0d6e78c..4476023 100644
            --- a/goodpoint.docx
            +++ b/goodpoint.docx
            @@ -1,7 +1,7 @@
             Making many good points
            
              1. Overview
            -- 2l3k23lk
            +- this is a test
             - 23lkjl2k3j
            
              2. Remarks
            

          编辑:在 git 2.7.4 上试过这个。如果不做更多工作,你不能在补丁中 checkoutadd

          【讨论】:

          • 接下来是什么,.xlsx,.pptx?!我认为另一种方法将从stackoverflow.com/a/32712206 中受益匪浅
          • 这对我有用,但有一些变化。对于第 1 步,我使用了pip install docx2txtd2t 没有工作,而不是步骤 #2、3 和 4。相反,我只需要运行命令git config --global diff.word.textconv docx2txt。第 5 步和第 6 步工作正常。我使用的是 git 版本 2.30.0,pip 21.0.1,它最终安装了 docx2txt 0.8。
          【解决方案6】:

          .docx 文件实际上是一个 zip 文件(您可以更改文件类型并在里面四处寻找)。如果.docx 被视为一个目录,那么主文件内部将存储为一个 XML 样式文件,它是文本,而不是二进制文件。

          遗憾的是没有回车。否则,对目录内的“document.xml”文件进行文本比较将非常有用。由于文件中的 XML 文件换行符不会影响内容,因此可以添加它们。

          【讨论】:

            【解决方案7】:

            这是有问题的,据我所知,这在 github 或任何其他 git 主机上是不可能的。虽然 git 可用于对任何内容进行版本控制,但 git diff 之类的内容将以纯文本形式返回两个版本的差异。难以辨认。

            我觉得这不是没有原因的。世界上有无限的文件格式,其中许多是专有的。因此,git 不支持 VLC 等每种格式,而是使用文本文件来处理所有内容。

            此外,即使 git 确实以某种方式支持 docx,它也无法在终端内显示格式更改,更不用说 cmd。如果只是文本,最好将其存储为文本文件。或手动签出以前的版本以比较更改。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-04-19
              • 1970-01-01
              • 1970-01-01
              • 2023-01-23
              • 2021-09-02
              • 2019-01-22
              • 2020-03-24
              • 2017-08-12
              相关资源
              最近更新 更多