在 SharePoint 上写入 Excel 个文件:由于重写而不是更新导致版本冲突

Writing Excel files on SharePoint: version conflicts due to rewrite instead of update

给定项目结构

.
├── source/
│   └── 1.xlsx
└── test.py

在本地同步的 SharePoint 目录中,1.xlsx 仅包含单元格 A1 中的字符串 one 和 test.py 包含:

import glob
import pandas as pd
import os

dir_path = os.path.dirname(os.path.realpath(__file__))

def main():
    file_list = glob.glob(rf"{dir_path}\source\*.xlsx")

    excel_list = []
    for file in file_list:
        excel_list.append(pd.read_excel(file))

    excl_merged = pd.concat(excel_list)

    writer = pd.ExcelWriter(rf"{dir_path}\merged.xlsx", engine='xlsxwriter')
    excl_merged.to_excel(writer, index=False)
    writer.save()

if __name__ == "__main__":
    main()

脚本是 运行 和 ./merged.xlsx 创建的。现在,该文件已在浏览器中打开。在 source 目录中创建包含字符串 two(单元格 A1)的 2.xlsx。该脚本是 运行 第二次。浏览器视图不会更新。 OneDrive 将显示同步错误:

如果merged.xlsx在本地打开,Excel会给出以下信息:

我猜同步问题是由于 Python 脚本完全重写了 merged.xlsx 造成的。 update merged.xlsx 是否有适当的解决方案而不是覆盖它并保持同步?

感谢jmcnamara 给xlwings 指点。以下似乎可以做到:

import glob
import pandas as pd
import os

dir_path = os.path.dirname(os.path.realpath(__file__))

def main():
    file_list = glob.glob(rf"{dir_path}\source\*.xlsx")

    excel_list = []
    for file in file_list:
        excel_list.append(pd.read_excel(file))

    excl_merged = pd.concat(excel_list)

    workbook = xw.Book(rf"{dir_path}\merged.xlsx")
    try:
        # named table does already exist
        workbook.sheets['Sheet1'].range('mytable').delete()
    except:
        # named table doesn't exist
        workbook.sheets['Sheet1'].range('A1').options(expand='table').delete()
    workbook.sheets['Sheet1'].range('A1').options(pd.DataFrame, header=1, index=False, expand='table').value = excl_merged
    workbook.sheets['Sheet1'].tables.add(source=workbook.sheets['Sheet1'].range('A1').expand(), name='mytable')
    workbook.save()

if __name__ == "__main__":
    main()