龙空技术网

利用openpyxl库编写插入excel指定行的函数

自强不息小红花re 168

前言:

现时小伙伴们对“python怎么添加空行”大概比较重视,兄弟们都想要分析一些“python怎么添加空行”的相关资讯。那么小编也在网摘上汇集了一些关于“python怎么添加空行””的相关内容,希望我们能喜欢,看官们一起来了解一下吧!

利用openpyxl库编写插入excel指定行的函数自主编写在python中使用第三方库库openpyxl实现插入修改Excel指定位置的多行,现编写如下函数实现def change_excel(self,star_row,del_rows,ins_row,ins_rows,content=[]);起始行位置 star_row删除行数 del_rows插入行位置 ins_row插入总行数 ins_rows插入内容 content=[]

def change_excel(self,star_row,del_rows,ins_row,ins_rows,content=[]):        # 单元格对齐样式        align = Alignment(horizontal='center',vertical='center',wrap_text=True)        align1 = Alignment(horizontal='left',vertical='center',wrap_text=True)        # 设置表格边框        border = Border(left=Side(border_style='thin',color='000000'),right=Side(border_style='thin',color='000000'),top=Side(border_style='thin',color='000000'),bottom=Side(border_style='thin',color='000000'))        try:            book = excl.load_workbook(self.filename)        except FileNotFoundError:            tkinter.messagebox.showinfo("提示", '无法保存,请打开Excel文件')            return False        sheet = book['Sheet']        # 删除的起始行        # star_row = 3        # 删除总行数        # del_rows = 3        sheet.delete_rows(star_row,del_rows+1)        # 插入位置        # ins_row = 3        #  插入总行数        # ins_rows = 3        # 插入空行语句        sheet.insert_rows(idx=ins_row, amount=ins_rows+1)        # 为插入空行赋值        i = 0        # print(content)        for row in sheet.iter_rows(ins_row,ins_row+ins_rows-1):            # print(row)            # for cell in row:            row[0].value = content[i][0]            row[0].alignment = align            row[0].border = border            row[1].value = content[i][1]            row[1].alignment = align            row[1].border = border            row[2].value = content[i][2]            row[2].alignment = align            row[2].border = border            row[3].value = content[i][3]            row[3].alignment = align            row[3].border = border            row[4].value = content[i][4]            row[4].alignment = align            row[4].border = border            row[5].value = content[i][5]            row[5].alignment = align            row[5].border = border            row[6].value = content[i][6]            row[6].alignment = align            row[6].border = bordersheet['H' + str(ins_row+i)].value = content[i][7]            sheet['H' + str(ins_row+i)].alignment = align            sheet['H' + str(ins_row+i)].border = border            sheet['I' + str(ins_row+i)].value = content[i][8]            sheet['I' + str(ins_row+i)].alignment = align            sheet['I' + str(ins_row+i)].border = border            """row[7].value = content[i][7]            row[7].alignment = align            row[7].border = border"""            """row[8].value = content[i][8]            row[8].alignment = align            row[8].border = border"""            i += 1            """设置行高"""            sheet.row_dimensions[ins_row+i].height = 25   # 设置行高        sheet['D' + str(ins_row+ins_rows)].value = "系统计算排烟量(m3)"        sheet['D' + str(ins_row+ins_rows)].alignment = align        sheet['D' + str(ins_row+ins_rows)].border = border        sheet['E' + str(ins_row+ins_rows)].value = self.xtjsl_py.get()        sheet['E' + str(ins_row+ins_rows)].alignment = align        sheet['E' + str(ins_row+ins_rows)].border = border        sheet['F' + str(ins_row+ins_rows)].value = "系统设计排烟量(m3)"        sheet['F' + str(ins_row+ins_rows)].alignment = align        sheet['F' + str(ins_row+ins_rows)].border = border        sheet['G' + str(ins_row+ins_rows)].value = self.xtsjl_py.get()        sheet['G' + str(ins_row+ins_rows)].alignment = align        sheet['G' + str(ins_row+ins_rows)].border = border        sheet.row_dimensions[ins_row+ins_rows].height = 25   # 设置行高                """设置列宽"""        sheet.column_dimensions['A'].width = 12        sheet.column_dimensions['B'].width = 16        sheet.column_dimensions['C'].width = 16        sheet.column_dimensions['D'].width = 18        sheet.column_dimensions['E'].width = 16        sheet.column_dimensions['F'].width = 18        sheet.column_dimensions['G'].width = 17        sheet.column_dimensions['H'].width = 17        sheet.column_dimensions['I'].width = 17        try:            book.save(self.filename)        except PermissionError:            print("提示", '无法保存,请关闭Excel文件')            return        print("提示", '已成功保存在'+self.filename+'文件中')        book.close()

标签: #python怎么添加空行 #insert函数py #python添加行