Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 875
Ed_F Avatar asked Dec 02 '25 09:12

Ed_F


1 Answers

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)
like image 129
adrtam Avatar answered Dec 04 '25 22:12

adrtam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!