Python Openpyxl, copy and paste a range of cells

I know how to select a range of cells in Openpyxl:

cell_range = sheet['A1':'C3']

How can I insert cell_range above into a different range, for example sheet['A11':'C13']?

+4
source share
2 answers

I don’t think that there is a slice notation exactly as described in your example, mainly because it openpyxluses lists Cell, not lists of simple values.

Here is the main thread that I would use to capture only part of the sheet.

>>> wb = openpyxl.load_workbook(file_name)
>>> sheet = wb.worksheets[0]
>>> rows = sheet.rows[1:3]                                      # slice rows
>>> foobar = [cell.value for row in rows for cell in row[3:10]] # slice row

EDIT: From this question, it looks like you can do something like this:

foo = [row[start_col:end_col] for row in sheet.rows[start_row:end_row]]

. foo[i][j].value Cell.

+2

. , .

0

Source: https://habr.com/ru/post/1652644/


All Articles