【问题标题】:openpyxl - read data validation - listopenpyxl - 读取数据验证 - 列表
【发布时间】:2016-11-09 13:00:24
【问题描述】:

我有一个 Excel 表格,带有数据验证属性 - 列表。

我需要制作一个包含 column_name 和相关下拉列表值的字典。

例如:{A:[a1, a2, a3], B:[b1, b2], C:[c1, c2, c3, c4]}。

如何在excel中嵌入数据校验列表有多个例子, 但是我该如何阅读呢?

我试过简单的代码:

from openpyxl import load_workbook

excel = load_workbook('test.xlsx')
sheet = excel.get_sheet_by_name('RequiredFormat')
for row in sheet.iter_rows():
    for cell in row:
        print(cell.value)
    input("Proceed ?")

我检查了单元格可用的选项。

但是没有什么可以说数据验证或列表。

有办法吗?

【问题讨论】:

  • 这看起来像是在尝试提供类似应用程序的功能。 openpyxl 特别关注文件格式。

标签: excel python-3.x openpyxl


【解决方案1】:

您目前无法读取现有数据验证。阅读 Openpyxl documentation

【讨论】:

  • 好吧,这不再是完全正确的,但它不会对您有太大帮助,因为与 openpyxl 中的所有公式一样,它们从未被评估过。但可能会看到对特定单元格进行了验证。
  • 现在好像有阅读功能了:bitbucket.org/openpyxl/openpyxl/issues/827/…
【解决方案2】:

正如@Jeanne Lane 在评论中指出的那样,有一些读取数据验证的能力:

https://foss.heptapod.net/openpyxl/openpyxl/-/issues/827#note_124765

for dv in worksheet.data_validations.dataValidation:
    # dv.type has the type, and other things here....

【讨论】:

    【解决方案3】:

    这是我将用于读取列表值和哪个单元格的代码快照

                    tstlst = ws.data_validations.dataValidation
                    print('---')
                    print(tstlst)
                    print('---')
                    nr = 0
                    for tst2 in tstlst:
                        nr += 1
                        print(str(nr) + " : " + str(tst2))
                        tst3 = tst2.sqref
                        print('sqref: ' + str(tst3))
                        tst4 = tst2.formula1
                        print('list: ' + tst4)
    

    也许这有帮助

    这会为我在 A13 中的数据验证列表生成以下输出

    2 : 参数: sqref=, showErrorMessage=True, showDropDown=None, showInputMessage=True, allowBlank=False, errorTitle=None, error=None, promptTitle=None, prompt=None, type='list', errorStyle=None , imeMode=None, operator=None, formula1='"val1,val2,val3"', formula2=None

    sqref: A13

    列表:“val1,val2,val3”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多