openpyxl - FutureWarning:此方法的行为将在未来版本中发生变化。使用特定的 'len(elem)' 或 'elem is not None' 测试
openpyxl - FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead
我在为 python 使用 openpyxl 加载 xlsm 文件时收到警告,然后 saving/closing 在我将一些数据添加到特定 sheet 中的特定 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)
问题是if not 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 加载 xlsm 文件时收到警告,然后 saving/closing 在我将一些数据添加到特定 sheet 中的特定 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)
问题是if not 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.