简体   繁体   中英

concatenate data in excel using openpyxl in Python

Here comes again, I have managed to collect specific data from resx file (xml) to generate an Excel file. Now, the task is to concatenate all the data to lowest row in this excel file.

First opening the input file to read(r) and write(w).

wb = load_workbook('Excel.xlsx')

access the worksheet named 'Sheet'

ws=get_sheet_by_name('Sheet')

So now, I need to Concatenate the data from all the cells in one column to the last empty cell of that column. Then generate this new excel file.

for example, Column Name: any column row1: ABC row2: EFG row3: HIJ

last row after concatenation should look like,

row4 : ABC EFG HIJ

As a python beginner this seems to be a pretty hard stuff for me. Please help to improve.

Thanks a lot.

Something like the following should work...

max_row = ws.get_highest_row() # find last row of worksheet
reff = "A1:A" + str(max_row) # build an Excel range covering the data
values = [cell.value for cell in ws.range(reff)] # collect the data
ws.cell('A' + str(max_row + 1)).value = ' '.join(values) # write values

The documentation for this module is pretty good. Look it over and experiment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM