import openpyxl

读取./double_data/44个双字.xlsx的第一列前44个数据存到数组data中

datafile = './doubledata/44个双字.xlsx' datawb = openpyxl.load_workbook(datafile, readonly=True) dataws = datawb.active data = []

for row in dataws.iter_rows(min_row=1, max_row=44, min_col=1, max_col=1): for cell in row: data.append(cell.value)

读取./doubledata/doubleresult_calligraphy.xlsx并进行处理

file = './doubledata/doubleresultcalligraphy.xlsx' wb = openpyxl.load_workbook(file) ws = wb.active

遍历从第2行到最后一行

for i in range(2, ws.max_row+1): # 嵌套遍历双数列 for j in range(2, ws.max_column, 2): cellvalue = ws.cell(row=i, column=j).value if cellvalue not in data: # 去掉第i行第j列和第i行第j+1列单元格 ws.delete_cols(j, 2)

# 重新遍历整个表格
for r in range(2, ws.max_row+1):
    for c in range(2, ws.max_column+1):
        # 将空数据按列向上填补
        if ws.cell(row=r, column=c).value is None:
            for k in range(r, 1, -1):
                if ws.cell(row=k, column=c).value is not None:
                    ws.cell(row=r, column=c).value = ws.cell(row=k, column=c).value
                    break

将更改后的表格存入矩阵中并保存在./doubledata/doubleresultcalligraphy2.xlsx中

wb.save('./doubledata/doubleresultcalligraphy2.xlsx'

请抄写以下代码import openpyxl读取double_data44个双字xlsx的第一列前44个数据存到数组data中datafile = doubledata44个双字xlsx datawb = openpyxlloadworkbookdatafile readonly=True dataws = datawbactive data = for row in datawsiterrows

原文地址: https://www.cveoy.top/t/topic/ddMo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录