title: Write to a specific Excel worksheet using python
body: <p>I want to overwrite specific cells in an already existing excel file. I've searched and found this answer, <a href="https://stackoverflow.com/questions/2725852/writing-to-existing-workbook-using-xlwt">writing to existing workbook using xlwt</a>. I've applied it as the following, </p>
  
  <pre><code>def wrtite_to_excel (self):
          #first I must open the specified excel file, sice open_file is in the same class, hence we can get it using self.sheet.
          bookwt = copy(self.workbook)
          sheetwt= bookwt.get_sheet(0)
          #now, I must know the column that was estimated so I overwrite it,
          colindex= self.columnBox.current() #returns the index of the estimated column
  
          for i in range (1, self.grid.shape[0]):
              if (str (self.sheet.cell_value(i,colindex)).lower () == self.missingBox.get().lower()):
                  #write the estimated value:
  
                       sheetwt.write (i, colindex, self.grid[i])
          bookwt.save(self.filename + '.out' + os.path.splitext(self.filename)[-1])
  </code></pre>
  
  <p>Notice that, self.workbook already exists in another method in the same class this way, </p>
  
  <pre><code>def open_file (self, file_name):
  
          try:
              self.workbook = xlrd.open_workbook(file_name)
  </code></pre>
  
  <p>I really don't know what this means, '.out' + os.path.splitext(self.filename)[-1], but it seems that it causes the modified file to be saved in the same path of the original one with a different name. </p>
  
  <p>After running the program, a new Excel file gets saved in the same path of the original one, however it is saved with a weird name as <code>data.xlsx.out.xlsx</code> and it doesn't open. I think it's caused by this line <code>'.out' + os.path.splitext(self.filename)[-1]</code>.  I removed that line in order to overwrite the original file and not saving a copy, but when running the program I become unable to open the original file and I get an error message saying that the file can't be opened because the file format or extension is not valid.  </p>
  
  <p>What I really want is to modify the original file not to create a modified copy. </p>
  
  <p>EDIT: SiHa's answer could modify the existing file without creating a copy if only the file name is specified like this, </p>
  
  <pre><code>bookwt.save(self.filename)
  </code></pre>
  
  <p>And, it could save a new copy this way, </p>
  
  <pre><code>filepath, fileext = os.path.splitext(self.filename)
  bookwt.save(filepath + '_out' + fileext)
  </code></pre>
  
  <p>Or as the line provided in my code in the question. However, in all of these methods the same problem exists, where after modifying the file it can't be opened. After searching I found that the problem could be solved by changing the extension of the original file from .xlsx to .xls. After making this change, the problem was solve. This is the link where I found the solution <a href="http://www.computing.net/answers/office/the-file-formatfile-extension-is-not-valid/19454.html" rel="nofollow noreferrer">http://www.computing.net/answers/office/the-file-formatfile-extension-is-not-valid/19454.html</a> </p>
  
  <p>Thank You. </p>