简体   繁体   English

在Python中使用openpyxl连接excel中的数据

[英]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. 再次出现,我已经设法从resx文件(xml)收集特定数据以生成Excel文件。 Now, the task is to concatenate all the data to lowest row in this excel file. 现在,任务是将所有数据连接到该excel文件的最低行。

First opening the input file to read(r) and write(w). 首先打开输入文件以读取(r)和写入(w)。

wb = load_workbook('Excel.xlsx') wb = load_workbook('Excel.xlsx')

access the worksheet named 'Sheet' 访问名为“ Sheet”的工作表

ws=get_sheet_by_name('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. 然后生成此新的excel文件。

for example, Column Name: any column row1: ABC row2: EFG row3: HIJ 例如,列名称:任何列row1:ABC row2:EFG row3:HIJ

last row after concatenation should look like, 串联后的最后一行应如下所示:

row4 : ABC EFG HIJ 第4行:ABC EFG HIJ

As a python beginner this seems to be a pretty hard stuff for me. 作为python初学者,这对我来说似乎是一件很难的事情。 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. 仔细看看并进行实验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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