wrap_text 使用 openpyxl。如何使用文档解决弃用警告?

wrap_text with openpyxl. How to use documentation to resolve deprecation warning?

我 运行 以下 openpyxl 命令将第 9 行之后的所有行中的文本换行。它工作正常但会引发弃用警告。我很想弄清楚如何使用 https://openpyxl.readthedocs.io/en/stable/ to determine the current, non-deprecated, way to wrap_text. But I always find the documentation confusing and unhelpful to me. For example, if I search for wrap_text I get this: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.styles.alignment.html#openpyxl.styles.alignment.Alignment.wrapText

等文档

但这并没有告诉我如何换行文本。我只是不知道如何使用文档吗?是否有什么大谜团要解开,这样我就不必无休止地 google 如何使用 openpyxl?如何查看此类文档并弄清楚如何在单元格中 wrap_text?

代码如下:

from openpyxl import load_workbook
from openpyxl.styles import Alignment

file1 = "C:\folder\inputFile1.xlsx"
wb=load_workbook(file1)
ws = wb.active
for rows in ws.iter_rows(min_row=10, max_row=None, min_col=None, max_col=None):
    for cell in rows:
       cell.alignment =  cell.alignment.copy(wrapText=True)
    wb.save('C:\folder\file1_wrap.xlsx')    

这里是弃用警告:

C:\Users\Jcurran\AppData\Local\Continuum\anaconda3\lib\site-packages\ipykernel_launcher.py:10: DeprecationWarning: 调用已弃用的函数副本(使用 copy(obj) 或 cell.obj = cell.obj + 其他)。 在我们加载内容时从 sys.path 移除 CWD。

我如何通过 https://openpyxl.readthedocs.io/en/stable/ 上的文档找到使用当前(未弃用)方法在单元格中包装文本所需的信息?

我在我的环境中使用 Jupyter。 Shift tab 或 tab 没有给我任何有用的信息。

有什么建议吗?我渴望自给自足,但无法掌握如何浏览文档以寻求答案。某处一定有什么线索?有些源码不知道怎么定位?

得知我没有最新的openpyxl版本。 pip install openpyxl 安装2.5。我升级到3.0了。 现在,当我查看 https://openpyxl.readthedocs.io/en/stable/index.html 时,它更有意义:-)

我现在知道 openpyxl 3.0 文档的 "Working with Styles" 部分是格式化数据的地方。

所以我点击它,然后转到 https://openpyxl.readthedocs.io/en/stable/styles.html

那个页面向我展示了这个:

>>> alignment=Alignment(horizontal='general',
...                     vertical='bottom',
...                     text_rotation=0,
...                     wrap_text=False,
...                     shrink_to_fit=False,
...                     indent=0)

我可以使用该信息用这一行来换行文本:

cell.alignment = Alignment(wrapText=True)

现在我开始明白了。 :-) 谢谢!