Python 使用pandas和openpyxl时出现Excel错误:修复的零件:/xl/worksheets/sheet1.xml带有xml错误的零件。HRESULT 0x8000ffff第1行第0列
我在写入excel文件后打开该文件时出错。这就是我到目前为止所做的:Python 使用pandas和openpyxl时出现Excel错误:修复的零件:/xl/worksheets/sheet1.xml带有xml错误的零件。HRESULT 0x8000ffff第1行第0列,python,excel,pandas,openpyxl,Python,Excel,Pandas,Openpyxl,我在写入excel文件后打开该文件时出错。这就是我到目前为止所做的: #locate source document Path = Path(r'C:\Users\username\Test\EXCEL_Test.xlsx') # open doc and go to active sheet wb = load_workbook(filename = Path) ws = wb.active #add drop down list to each cell in a certain co
#locate source document
Path = Path(r'C:\Users\username\Test\EXCEL_Test.xlsx')
# open doc and go to active sheet
wb = load_workbook(filename = Path)
ws = wb.active
#add drop down list to each cell in a certain column
dv_v = DataValidation(type="list", formula1='"Y,N"', allow_blank=True)
for cell in ws['D']:
cell = ws.add_data_validation(dv_v)
wb.save(Path)
以下是打开excel文件时出现的两个错误:
第一个错误弹出窗口:
我们发现“EXCEL_Test.xlsx”中的某些内容有问题。是否希望我们尽可能多地恢复?如果您信任此工作簿的来源,请单击“是”
第二个错误弹出窗口:
修复的零件:/xl/worksheets/sheet1.xml有xml错误的零件。HRESULT 0x8000ffff第1行第0列
“我的数据验证”未显示,并且在尝试打开文件以查看openpyxl更改时,该文件存在上述错误
也许有人能帮我找出为什么会出现这些错误?Python以退出代码0结束,为什么数据验证在恢复的文件中显示为空白?我认为您使用的是
ws.add\u data\u validation(dv)
错误。首先将数据验证分配给dv,然后将dv添加到单元格中。
试着这样做
import openpyxl
from openpyxl import Workbook
from openpyxl.worksheet.datavalidation import DataValidation
#locate source document
Path = 'C:/Users/username/test/Excel_test.xlsx'
# open doc and go to active sheet
wb = openpyxl.load_workbook(filename = Path)
ws = wb['Sheet1']
#add drop down list to each cell in a certain column
dv = DataValidation(type="list", formula1='"Y,N"', allow_blank=True)
ws.add_data_validation(dv)
# This is the same as for the whole of column D
dv.add('D1:D1048576')
wb.save(Path)
看看这里的文档:我也试过了,但在excel中也出现了同样的错误:dv_ef=DataValidation(type=“list”,formula1='“Y,N”',allow_blank=True,showDropDown=True)ws.add_data_validation(dv_ef)您还有这个问题吗?