删除列数据
使用openpyxl库操作Excel,可以删除指定的列
import openpyxl file_path = r"C:\Users\Desktop\test.xlsx" wb = openpyxl.load_workbook(filename) # 读取Excel文件 ws = wb.active # 激活Excel ws.delete_cols(4) # 删除第4列数据 ws.delete_cols(3) # 删除第3列数据 wb.save(file_path) # 保存文件
往指定单元格中写入超链接
xlsxwriter库也是非常的强大,使用xlsxwriter库操作Excel,可以实现datatime,url,boolean等等方法,如下:
安装库
pip3 install xlsxwriter
调用库,操作Excel
import xlsxwriter # 创建一个新工作簿 workbook = xlsxwriter.Workbook(r"C:\Users\Desktop\test.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
格式
后续更新中。。。。
原文地址:https://www.cnblogs.com/rongge95500/p/11453507.html
时间: 2024-10-27 19:30:32