Python 3 openpyxl UserWarning:不支持数据验证扩展

Tom*_*_99 10 python io openpyxl

所以这是我第一次尝试从 Excel 文件中读取数据,并且尝试使用 openpyxl 模块进行读取。我的目标是整理一个带有嵌套列表作为其值的字典。但是,当我尝试运行它时收到此警告时:

用户警告:不支持数据验证扩展,将被删除警告(味精)

我不知道我哪里出错了。任何帮助将非常感激。谢谢

import openpyxl
try:
    wb = openpyxl.load_workbook("Grantfundme Master London.xlsx")
    except FileNotFoundError:
        print("File could not be found.")

sheet = wb["FUNDS"]

database = {}
for i in range(250):#this is the number of keys I want in my dictionary so loop through rows 
    charity = sheet.cell(row=i + 1, column=1).value

    area_of_work = []
    org = []
    funding = sheet.cell(row=i + 1, column=14).value

    for x in range(8, 13): # this loops through columns with info I need
        if sheet.cell(row=i +1, column=x).value !="":
            area_of_work.append(sheet.cell(row=i +1, column=x).value)

    for y in range(3, 6): # another column loop
        if sheet.cell(row=i +1, column=y).value !="":
            org.append(sheet.cell(row=i +1, column=y).value)

    database[charity] = [area_of_work,org, funding]

try:
    f = open("database.txt", "w")
    f.close()
except IOError:
    print("Ooops. It hasn't written to the file")
Run Code Online (Sandbox Code Playgroud)

对于那些在这里询问的人,这是异常的屏幕截图:(数据验证期望

S3D*_*DEV 22

有时,简单地清除工作簿中的数据验证规则并不是一个可行的解决方案 - 也许其他用户依赖这些规则,或者他们可能被锁定以进行编辑等。

可以使用简单的过滤器忽略该错误,并且工作簿可以保持不变,如下所示:

import warnings

warnings.simplefilter(action='ignore', category=UserWarning)
Run Code Online (Sandbox Code Playgroud)

实际上,这可能看起来像:

import pandas as pd
import warnings

def load_data(path: str):
    """Load data from an Excel file."""
    warnings.simplefilter(action='ignore', category=UserWarning)
    return pd.read_excel(path)
Run Code Online (Sandbox Code Playgroud)

注意: 请记住重置 warnings,否则所有其他 UserWarnings 也将被忽略。


Mik*_*e T 7

使用warnings.catch_warning上下文管理器暂时忽略警告,如下所示:

import warnings
import pandas as pd

with warnings.catch_warnings():
    warnings.filterwarnings("ignore", category=UserWarning)
    df = pd.read_excel("file.xlsx")

# now warning filter is restored
Run Code Online (Sandbox Code Playgroud)


小智 5

Excel 有一个称为数据验证的功能(在我的版本中数据选项卡的数据工具部分),您可以在其中从规则列表中进行选择以限制可以在单元格中输入的数据类型。这有时用于在 Excel 中创建下拉列表。此警告告诉您 openpyxl 不支持此功能,并且不会强制执行这些规则。如果您希望警告消失,您可以单击 Excel 中的“数据验证”图标,然后单击“全部清除”按钮以删除所有数据验证规则并保存您的工作簿。

  • 很好的解释,但是对于某些人来说这不是一个可行的解决方案。如果使用了工作表并且需要数据验证规则怎么办?简单地擦除它们是具有破坏性的,并且可能会影响其他用户。 (14认同)

小智 0

谢谢,您的截图!如果没有看到实际的 Excel 工作簿,很难确切地说出它在抱怨什么。

如果您注意到屏幕截图引用了阅读器工作表模块的第 322 行。它看起来像是在告诉您 openpyxl 库不支持 OOXML 标准的数据验证扩展。它似乎在说它在您的工作簿中找到了部分数据验证扩展,并且在使用 openpyxl 扩展解析工作簿时将会丢失。

  • @9a3eedi您可以使用“警告”模块抑制它:https://docs.python.org/3/library/warnings.html (4认同)