python有没有可以修改已存在excel文件的模块
仅供参考,以下代码可以在保留原格式的情况下修改EXCEL表内容,但是表格中的公式和图片都会消失掉。
import xlrd
import xlutils.copy
newwb = xlrd.open_workbook('output.xls', formatting_info=True) # formatting_info 带格式导入
outwb = xlutils.copy.copy(newwb) # 建立一个副本来用xlwt来写
# 修改值
def setOutCell(outSheet, col, row, value):
""" Change cell value without changing formatting. """
def _getOutCell(outSheet, colIndex, rowIndex):
""" HACK: Extract the internal xlwt cell representation. """
row = outSheet._Worksheet__rows.get(rowIndex)
if not row: return None
cell = row._Row__cells.get(colIndex)
return cell
# HACK to retain cell style.
previousCell = _getOutCell(outSheet, col, row)
# END HACK, PART I
outSheet.write(row, col, value)
# HACK, PART II
if previousCell:
newCell = _getOutCell(outSheet, col, row)
if newCell:
newCell.xf_idx = previousCell.xf_idx
# END HACK
outSheet = outwb.get_sheet(0)
setOutCell(outSheet, 5, 5, 'Test')
outwb.save('output.xls')
广告 您可能关注的内容 |