Ed_F
Ed_F

Reputation: 185

Openpyxl: How to merge cells using variable rows

I can't figure out how to merge cells without using the format 'A1:A4'

I want to be able to use ws.cell(row=x,column=y) format to set the start and end points of the merge.

The code below demonstrates what I want, but doesn't work as the range argument isn't in the correct format.

for x in range(10):
    start = ws.cell(row=x,column=1)
    end = ws.cell(row=x, column=4)
    ws.merge_cells(start:end)

Any help much appreciated!

Upvotes: 17

Views: 38673

Answers (2)

hamid sassi
hamid sassi

Reputation: 11

ws.merge_cells("E4:G4")

Upvotes: 1

adrtam
adrtam

Reputation: 7241

See the manual, we have the following:

merge_cells(range_string=None, start_row=None, start_column=None, end_row=None, end_column=None)

so what you need to do is to do this instead:

for x in range(10):
    ws.merge_cells(start_row=x, start_column=1, end_row=x, end_column=4)

Upvotes: 32

Related Questions