[英]Printing value of a cell in openpyxl
I was writing a simple python program using openpyxl
and I'm just a beginner trying to learn more about python and it's modules as it's confusing.我正在使用openpyxl
编写一个简单的 python 程序,而我只是一个初学者,试图了解更多关于 python 及其模块的信息,因为它令人困惑。
I wanted to print the value of a cell and I eventually landed at the doc page我想打印一个单元格的值,我最终登陆了文档页面
https://openpyxl.readthedocs.io/en/stable/api/openpyxl.cell.cell.html?highlight=value#openpyxl.cell.cell.Cell.value https://openpyxl.readthedocs.io/en/stable/api/openpyxl.cell.cell.html?highlight=value#openpyxl.cell.cell.Cell.value
I would simply like to know what我只是想知道什么
class openpyxl.cell.cell.Cell(worksheet, row=None, column=None, value=None, style_array=None)
actually means over here.实际上是这里的意思。 I know it says openpyxl.cell.cell
module at the top but really I find it confusing, does this mean that this is a class in a module in a module or have I got it entirely wrong?我知道它在顶部说openpyxl.cell.cell
模块,但我真的觉得它令人困惑,这是否意味着这是一个模块中的 class 或者我完全错了?
Right now I'm using code like:现在我正在使用如下代码:
import openpyxl as xl
from openpyxl import Workbook
wb = xl.load_workbook('xyz.xlsx')
ws = wb['Sheet1']
print(ws.cell(row=1,column=1).value})
My question is how does the last line even work?我的问题是最后一行是如何工作的? Shouldn't I use xl.cell.cell.value
instead of ws.cell(row=1,column=1).value
?我不应该使用xl.cell.cell.value
而不是ws.cell(row=1,column=1).value
吗? I'm sorry but I don't understand.我很抱歉,但我不明白。 Any help would be much appreciated.任何帮助将非常感激。
Use利用
import openpyxl
print( openpyxl.__file__ )
to find path to source code (skip __init__.py
)查找源代码的路径(跳过__init__.py
)
On Linux I have /usr/local/lib/python3.7/dist-packages/openpyxl/
and there is folder cell
with file cell.py
which has class Cell
- and this is your class openpyxl.cell.cell.Cell
在 Linux 我有/usr/local/lib/python3.7/dist-packages/openpyxl/
并且有一个文件夹cell
文件cell.py
有class Cell
- 这是你的class openpyxl.cell.cell.Cell
单元格。
There is no没有
xl.cell.cell.value
but但
xl.cell.cell.Cell(...).value
or或者
mycell = xl.cell.cell.Cell(...)
mycell.value
but this creates standalone cell
which is not part of workspace
so it is useless for you.但这会创建独立的cell
,它不是workspace
的一部分,因此对您来说毫无用处。
When you read file then it creates many Cell()
and keep all of them in other class Workspace
(maybe as list/dict of cells or array of cells) and Workspace
has method cell(row,column)
(it is not class Cell
, nor module cell
) to give you access to one of Cell()
.当您读取文件时,它会创建许多Cell()
并将它们全部保存在其他 class Workspace
(可能作为单元格列表/字典或单元格数组)并且Workspace
具有方法cell(row,column)
(它不是class Cell
,也不模块cell
)让您访问Cell()
之一。
So use所以使用
ws.cell(row=1,column=1).value
because it gives you access to cells readed from file.因为它使您可以访问从文件中读取的单元格。
Doc can be useful to see what other functions you can use with ws.cell(row=1,column=1)
- ie Doc 可用于查看您可以与ws.cell(row=1,column=1)
一起使用的其他功能 - 即
mycell = ws.cell(row=1,column=1)
print( mycell.value )
print( mycell.column_letter )
print( mycell.column )
print( mycell.row )
print( mycell.offset(row=1,column=1) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.