4

I know how to select a cell range in Openpyxl:

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

How can I paste cell_range above to another range, like sheet['A11':'C13']?

1
  • AFAIK you can't copy specific cells but I could be wrong. I think it's possible to copy an entire worksheet though. Commented Aug 26, 2016 at 19:00

2 Answers 2

3

I don't think there is a slicing notation exactly as described in your example, mostly because openpyxl uses lists of Cells rather than lists of plain values.

Here is the basic flow I would use to grab just a part of a 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]]

Which will give you a list of lists of cells. Remember to use foo[i][j].value to get the contents of a cell.

Sign up to request clarification or add additional context in comments.

Comments

0

You might want to look at this snippet for some ideas. Please note it is entirely unsupported.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.