如何使用Git 对Excel 文件进行版本管理

551 阅读1分钟
  1. 安装

  • 安装需要的工具【这一步是 MAC 的安装方式,Windows 不是很熟悉,请自行配置】
brew install git # 如果电脑没有git,需要安装
brew install exiftool 
brew install pandoc     
  • 安装 python 包

我这里用的是 pipenv

pipenv install openpyxl
pipenv install textract     
pipenv install Pandas

pipenv graph  # 检查安装结果
  1. 修改配置文件

  • 在对应项目下新增 .gitattributes 配置文件
vi .gitattributes

文件内容:

*.docx diff=word
*.pptx diff=powerpoint
*.xlsx diff=excel
*.pdf diff=pdf
*.jpg diff=images_videos
*.png diff=images_videos
*.gif diff=images_videos
*.mp4 diff=images_videos
  • 在对应项目 .git/config 文件添加以下内容
                                                                                                                                                                 
[alias]                                                                                                                                                               
    wdiff = diff --word-diff=color --unified=1                                                                                                                        
                                                                                                                                                                      
[diff "excel"]                                                                                                                                                        
    textconv=python /xx(your file path)/xlsx-dump.py                                                                                                           
    binary=true  
  • xlsx-dump.py 内容
from io import StringIO
import sys

import pandas as pd

for sheet_name in pd.ExcelFile(sys.argv[1]).sheet_names:
    output = StringIO()
    print('Sheet: %s' % sheet_name)
    pd.read_excel(sys.argv[1], sheet_name=sheet_name)\
      .to_csv(output,
              header=True,
              index=False)
    print(output.getvalue())
  1. 验证

# 激活虚拟环境
pipenv shell
git log -p --word-diff=color xxx(your file).xlsx  
# 退出虚拟环境
exit

配合其他客户端工具(比如 github.com/desktop/des… github 集成应该可以有可视化的展现结果,我这里没安装,就不赘述了。【不过 vscode 暂时没找到合适的插件可以可视化展示,有的话麻烦踢我下】

  1. 参考资料

tech.marksblogg.com/git-track-c…