删除Excel指定行或列
使用openpyxl库操作Excel,可以删除指定的列
import openpyxl def excel_delete(fp): wb = openpyxl.load_workbook(fp) # 读取Excel文件 ws = wb.active # 获取激活的sheet页 ws.delete_cols(idx=4, amount=3) # 从第4列开始往后删除三列 ws.delete_rows(idx=2, amount=2) # 从第2行开始往下删除两行 wb.save(fp) # 保存文件
往Excel中追加数据
只往Excel的最后一行追加数据
import openpyxl def write_append(io, data): """ 追加写入数据 :param io: Excel文件 :param data: 数据,list 或 dict类型 :return: """ wb = openpyxl.load_workbook(io) sht = wb.active sht.append(data) wb.save(io)
往Excel中插入行或列
import openpyxl def excel_insert(fp): ''' 插入的行和列都不是按索引插入的,是实际的行和列 偏移的行和列都是从头(A1位置)开始算的 :param fp: 文件路径 ''' wb = openpyxl.load_workbook(fp) ws = wb.active ws.insert_cols(2) # 插入第2列,即B列 ws.insert_cols(2, 4) # 向右偏移4列,插入第5列,即E列 ws.insert_rows(2) # 插入第2行 ws.insert_rows(2, 3) # 向下偏移3行,插入第4行 wb.save(fp)
往指定单元格中写入超链接
xlsxwriter库也是非常的强大,使用xlsxwriter库操作Excel,可以实现datatime,url,boolean等等方法,如下:
安装库
pip3 install xlsxwriter
调用库,操作Excel
import xlsxwriter # 创建一个新工作簿 workbook = xlsxwriter.Workbook(r"C:UsersDesktop est.xlsx") # 添加一个工作表 worksheet = workbook.add_worksheet('test') worksheet.set_column('B:B', 40) # 为B列设置列宽为40 worksheet.set_row(2, 30) # 为第二行设置行高为30 # 设置样式 formats = workbook.add_format({ 'font_color': 'yellow', 'bold': 2, 'underline': 1, 'font_size': 12, 'fg_color': 'red' }) # 写入超链接 worksheet.write_url('B1', 'https://www.baidu.com/') # 隐式显示 worksheet.write_url('B2', 'https://www.baidu.com/', string='百度一下') # 显示string worksheet.write_url('B3', 'https://www.baidu.com/', tip='前往百度') # 鼠标悬浮提示信息 worksheet.write_url('B4', 'https://www.baidu.com/', cell_format=formats) # 按格式显示 worksheet.write_url('B5', 'C:/files/file') worksheet.write_url(1, 2, 'https://www.baidu.com/', cell_format=formats, string='hello', tip='click') # 写入一个非超链接的URL worksheet.write_string('B6', 'http://www.baidu.com/') workbook.close()
效果如下:
可以设置的格式大概有下面这么多,基本还是够用的
self.xf_format_indices = xf_indices self.dxf_format_indices = dxf_indices self.xf_index = None self.dxf_index = None self.num_format = 'General' self.num_format_index = 0 self.font_index = 0 self.has_font = 0 self.has_dxf_font = 0 self.bold = 0 self.underline = 0 self.italic = 0 self.font_name = 'Calibri' self.font_size = 11 self.font_color = 0x0 self.font_strikeout = 0 self.font_outline = 0 self.font_shadow = 0 self.font_script = 0 self.font_family = 2 self.font_charset = 0 self.font_scheme = 'minor' self.font_condense = 0 self.font_extend = 0 self.theme = 0 self.hyperlink = False self.xf_id = 0 self.hidden = 0 self.locked = 1 self.text_h_align = 0 self.text_wrap = 0 self.text_v_align = 0 self.text_justlast = 0 self.rotation = 0 self.fg_color = 0 self.bg_color = 0 self.pattern = 0 self.has_fill = 0 self.has_dxf_fill = 0 self.fill_index = 0 self.fill_count = 0 self.border_index = 0 self.has_border = 0 self.has_dxf_border = 0 self.border_count = 0 self.bottom = 0 self.bottom_color = 0 self.diag_border = 0 self.diag_color = 0 self.diag_type = 0 self.left = 0 self.left_color = 0 self.right = 0 self.right_color = 0 self.top = 0 self.top_color = 0 self.indent = 0 self.shrink = 0 self.merge_range = 0 self.reading_order = 0 self.just_distrib = 0 self.color_indexed = 0 self.font_only = 0
后续更新中。。。。