我并不是真的使用Python来实现这一点,但这里有一个比较简单的方法,我很快就找到了。import openpyxl
wb = openpyxl.load_workbook('test.xlsx')
ws1 = wb.active
names = []
for row in ws1.columns[0]:
names.append(row.value)
names = sorted(list(set(names)))
start = 1
for name in names:
ws1.cell(row = start, column=2).value = name
start += 1
wb.save('test.xlsx')
编辑:显然更新的openpyxl需要稍作修改
更改此:for row in ws1.columns[0]:
names.append(row.value)
对此:for row in ws1.iter_cols(max_col = 1, min_row=1):
for cell in row:
names.append(cell.value)
万一你的专栏不一样iter_cols(min_col=None, max_col=None, min_row=None, max_row=None)[source]
Returns all cells in the worksheet from the first row as columns.
If no boundaries are passed in the cells will start at A1.
If no cells are in the worksheet an empty tuple will be returned.
Parameters:
min_col (int) – smallest column index (1-based index)
min_row (int) – smallest row index (1-based index)
max_col (int) – largest column index (1-based index)
max_row (int) – smallest row index (1-based index)