gpt4 book ai didi

python - openpyxl - FutureWarning : The behavior of this method will change in future versions. 使用特定的 'len(elem)' 或 'elem is not None' 测试代替

转载 作者:行者123 更新时间:2023-12-02 05:35:44 25 4
gpt4 key购买 nike

使用 python 的 openpyxl 加载 xlsm 文件,然后在将一些数据添加到特定工作表中的特定 7 个单元格后保存/关闭它时,我收到警告。问题是我收到了一个“FutureWarning”,我不知道它是什么。我已经搜索这个有一段时间了,但一直无法破译。

我怀疑 wb.save() 方法是触发此警告的原因,因为当我评论此特定行时它没有显示。

有人知道这是什么吗?

代码

wb = openpyxl.load_workbook(filename=directory_path.xlsm, keep_vba=True)
ws = wb['sheetname']
ws.cell(row1, col1).value = ### (some number)
ws.cell(row2, col2).value = ### (some number)
ws.cell(row3, col3).value = ### (some number)
ws.cell(row4, col4).value = ### (some number)
ws.cell(row5, col5).value = ### (some number)
ws.cell(row6, col6).value = ### (some number)
ws.cell(row7, col7).value = ### (some number)
wb.save(directory_path.xlsm)
wb.close()

警告消息

C:\Users\...\Anaconda3\lib\site-packages\openpyxl\comments\shape_writer.py:75: FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead.
if not shape_types:

最佳答案

Openpyxl 似乎使用的是旧版本的 lxml。这是您的 lxml 版本中的警告。

shape_writer 的第 73-76 行源代码为:

# check whether comments shape type already exists
shape_types = root.find("{%s}shapetype[@id='_x0000_t202']" % vmlns)
if not shape_types:
self.add_comment_shapetype(root)

问题在于如果不是shape_types:root.find() 是对 lxml 的调用。 documentation for lxml说:

Prior to ElementTree 1.3 and lxml 2.0, you could also check the truth value of an Element to see if it has children, i.e. if the list of children is empty:

if root:   # this no longer works!
print("The root element has children")

This is no longer supported as people tend to expect that a "something" evaluates to True and expect Elements to be "something", may they have children or not. So, many users find it surprising that any Element would evaluate to False in an if-statement like the above. Instead, use len(element), which is both more explicit and less error prone.

关于python - openpyxl - FutureWarning : The behavior of this method will change in future versions. 使用特定的 'len(elem)' 或 'elem is not None' 测试代替,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51645361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com