如果我有使用 openpyxl 的行号,我如何迭代 excel 工作表的特定行?
how can I iterate specific rows of excel worksheet if I have row numbers using openpyxl?
我是编程新手,目前正在学习 python 和用于 excel 文件操作的 openpyxl。
我正在编写一个脚本来帮助更新修复数据库,该数据库在 excel sheet 中选择特定记录。
我写了一个脚本,我可以在其中获取我需要更新的 excel 数据的行号,但现在的挑战是如何通过迭代在行(记录)列表中创建列表。
例如,我发现我需要第 22、23、34 和 35 行的数据。有没有一种方法可以获取这些行的数据而无需更改 min_row 和 max_row每个行实例的编号?
我需要重写的代码部分是:
# copy the record on the rows of the row numbers found above.
rows_records = []
row_record = []
for row_cells in ws2.iter_rows(min_row=23, max_row=23, min_col=1, max_col = 6):
for cell in row_cells:
row_record.append(cell.value)
rows_records.append(row_record)
print(row_record)
print(rows_records)
enter image description here
import openpyxl
book = openpyxl.load_workbook('numbers.xlsx')
sheet = book.active
rows = sheet.rows
values = []
your_row_indices = [21,22,23]
for row in rows[your_row_indices]:
for cell in row:
values.append(cell.value)
自从 openpyxl return 生成器以来,您必须将其转换为列表才能访问特定的索引
import openpyxl
workbook = openpyxl.load_workbook('numbers.xlsx')
worksheet = workbook .active
rows = list(worksheet.iter_rows(max_col=6, values_only=True))
values = []
row_indices = [21, 22, 23, 34, 35]
for row_index in row_indices:
values.append(rows[row_index])
我是编程新手,目前正在学习 python 和用于 excel 文件操作的 openpyxl。 我正在编写一个脚本来帮助更新修复数据库,该数据库在 excel sheet 中选择特定记录。 我写了一个脚本,我可以在其中获取我需要更新的 excel 数据的行号,但现在的挑战是如何通过迭代在行(记录)列表中创建列表。
例如,我发现我需要第 22、23、34 和 35 行的数据。有没有一种方法可以获取这些行的数据而无需更改 min_row 和 max_row每个行实例的编号?
我需要重写的代码部分是:
# copy the record on the rows of the row numbers found above.
rows_records = []
row_record = []
for row_cells in ws2.iter_rows(min_row=23, max_row=23, min_col=1, max_col = 6):
for cell in row_cells:
row_record.append(cell.value)
rows_records.append(row_record)
print(row_record)
print(rows_records)
enter image description here
import openpyxl
book = openpyxl.load_workbook('numbers.xlsx')
sheet = book.active
rows = sheet.rows
values = []
your_row_indices = [21,22,23]
for row in rows[your_row_indices]:
for cell in row:
values.append(cell.value)
自从 openpyxl return 生成器以来,您必须将其转换为列表才能访问特定的索引
import openpyxl
workbook = openpyxl.load_workbook('numbers.xlsx')
worksheet = workbook .active
rows = list(worksheet.iter_rows(max_col=6, values_only=True))
values = []
row_indices = [21, 22, 23, 34, 35]
for row_index in row_indices:
values.append(rows[row_index])