[英]openpyxl to read from Excel file
I have some code which I want to use to iterate through some rows in excel, and for this i openpyxl.我有一些代码,我想用它来遍历 excel 中的一些行,为此我使用 openpyxl。
I would like to, for each row, print something like this:我想为每一行打印如下内容:
'The product id is: ' + column 1 + 'and the product is: ' + column 2
But I don't know how to distinguish between the columns in the code.但我不知道如何区分代码中的列。 The code I have right now is the following:
我现在拥有的代码如下:
from openpyxl import load_workbook
file = load_workbook('path...file.xlsx')
list = file.active
for value in list.iter_rows(min_row=1, min_col=1, max_col=2, values_only=True):
print(value)
And the result I get is the following:我得到的结果如下:
('ProductID', 'Product')
('xxx', 'xxx')
...
('yyy', 'yyy')
So I can use the entire row, but I'm unsure as to how I should refer to each individual cell in the row when I print.所以我可以使用整行,但我不确定在打印时应该如何引用行中的每个单独的单元格。
Is there a different way I should do this?我应该这样做吗?
Please try this:请试试这个:
from openpyxl import load_workbook
file = load_workbook('path...file.xlsx')
sheet = file.active
for row in range(sheet.max_row):
cell = sheet.cell(row=row, column=1)
print(cell.value)
you could do open file and use readlines() then iterate through the lines and use the comma separate the values in to list and use for eg value[0] as id and value[1] as product您可以打开文件并使用 readlines() 然后遍历行并使用逗号分隔值以列出并使用例如 value[0] 作为 id 和 value[1] 作为产品
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.