《Python编程快速上手+让繁琐工作自动化》第12章实践项目:空行插入程序
import openpyxl ''' 空行插入程序 ''' def insert_none(n, m, filename): wb = openpyxl.load_workbook(filename) sheet = wb.active wb_new = openpyxl.Workbook() sheet_new = wb_new.active # 复制n行前的数据 for i in range(1, n): for j in range(1, sheet.max_column+1): sheet_new.cell(row=i, column=j).value = sheet.cell(row=i, column=j).value # n行后数据写入新的表格中 for i in range(n, sheet.max_row+1): for j in range(1, sheet.max_column+1): sheet_new.cell(row=i+m, column=j).value = sheet.cell(row=i, column=j).value wb_new.save('new.xlsx') if __name__ == '__main__': insert_none(2, 5, 'multiplication.xlsx')