简体   繁体   English

如何在python中使用openpyxl访问列

[英]How to access columns using openpyxl in python

I am trying to access a column from a workbook.我正在尝试从工作簿访问列。 Im able to do it using this:我可以使用这个来做到这一点:

for cell in sheet1["A"]:
    list.append(cell.value)

However, this collects all the value on the A column.但是,这会收集 A 列上的所有值。 What I need starts from A2 and on.我需要的东西从 A2 开始。 I this code:我这个代码:

for cell in sheet1["A2":"A"]
    list.append(cell.value)

What am I missing here?我在这里缺少什么?

A super simple idea to fix your code is to use len() as in the following;修复代码的一个超级简单的想法是使用len() ,如下所示;

for cell in sheet1['A1':'A'+str(len(sheet['A']))]
    list.append(cell.value)

Remember that str() converts its argument to a string and + between two strings concatenates them.请记住, str()将其参数转换为字符串,并且两个字符串之间的+将它们连接起来。 len(sheet['A']) returns the length of the column A. len(sheet['A'])返回 A 列的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM