如果我编辑没有外部查询的XLSM文件,我的代码可以正常工作。如果我尝试编辑一个具有外部链接的文件,则链接会被断开/删除,并且会出现以下错误:
<logFileName>error205520_01.xml</logFileName>
-<removedParts summary="Following is a list of removed parts:">
<removedPart>Removed Part: External data range.</removedPart>
</removedParts>
我的代码如下:
from openpyxl import load_workbook
import pandas as pd
df_new = pd.DataFrame({'Col_C': [9, 10, 11, 12]})
wb = load_workbook('2020data.xlsm', keep_vba=True)
ws = wb['testSheet']
for index, row in df_new.iterrows():
cell = 'C%d' % (index + 2)
ws[cell] = row[0]
wb.save('2020data.xlsm')