栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用openpyxl插入列

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用openpyxl插入列

尚未

.EntireColumn.Insert
在openpyxl中找到任何东西。

我首先想到的是通过修改工作表上的_cell手动插入列。我认为这不是插入列的最佳方法,但是它可以工作:

from openpyxl.workbook import Workbookfrom openpyxl.cell import get_column_letter, Cell, column_index_from_string, coordinate_from_stringwb = Workbook()dest_filename = r'empty_book.xlsx'ws = wb.worksheets[0]ws.title = "range names"# inserting sample datafor col_idx in xrange(1, 10):    col = get_column_letter(col_idx)    for row in xrange(1, 10):        ws.cell('%s%s' % (col, row)).value = '%s%s' % (col, row)# inserting column between 4 and 5column_index = 5new_cells = {}ws.column_dimensions = {}for coordinate, cell in ws._cells.iteritems():    column_letter, row = coordinate_from_string(coordinate)    column = column_index_from_string(column_letter)    # shifting columns    if column >= column_index:        column += 1    column_letter = get_column_letter(column)    coordinate = '%s%s' % (column_letter, row)    # it's important to create new Cell object    new_cells[coordinate] = Cell(ws, column_letter, row, cell.value)ws._cells = new_cellswb.save(filename=dest_filename)

我了解此解决方案非常丑陋,但希望它能帮助您朝正确的方向思考。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645874.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号