I have a excel sheet and I am trying write data into that. My sheet works as a template and after writing I will save the file with another name. My python code for populating is like this
# -*- coding: utf-8 -*-
import json
import xlrd
from xlutils.copy import copy
import os
book = xlrd.open_workbook('file.xls',formatting_info=True)
wb = copy(book)
sheet1=wb.get_sheet(0)
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
def setOutCell(outSheet, col, row, value):
""" Change cell value without changing formatting. """
# 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
# Populate the data
setOutSheet(sheet1,2,3,value)
# Save the file
wb.save("test.xls")
After executing the code the border in my excel sheet are distorted and data is not getting populated properly. I found out that the sheet is getting damaged at the line copy(book) itself can anyone help in solving this. Here is the link of the screen shot after getting the output image