title: How to add sheet to excel when called inside a function?
body: <p>I have written a code in python that calls stock market data from an API, performs some functions and puts the results in an excel sheet. the only problem is whenever I call this function, it overwrites the new data on the existing sheet instead of creating a new sheet and entering the data into that. The function looks like this:</p>
  <pre><code>     def getCSV(self, symbols):
          for i in symbols:
              getIntradayData(i, self.shorttrend, self.longtrend, self.interval)
              
          text=self.exchangeName
          x = text.split(&quot;&gt;&quot;)
          text=x[1]
          workbook = xlsxwriter.Workbook('tradingviewdata.xlsx')
          worksheet = workbook.add_worksheet(text) 
          bold = workbook.add_format({'bold': 1})
          worksheet.set_column(1, 1, 15)
  
          worksheet.write('A1', 'Symbol', bold)
          worksheet.write('B1', 'Code', bold)
          worksheet.write('C1', 'Code Short', bold)
          
          row = 1
          col = 0   
  
          for Symbol, Code, cs in (listfinal):
              try:
                  worksheet.write_string (row, col,Symbol)
                  worksheet.write_string(row, col + 1, Code)
                  worksheet.write_string(row, col + 2, cs)
                  
                  row += 1
              except:
                  pass
  
          workbook.close()
  </code></pre>
  <p>Here, <code>symbols</code> is a list of stock symbols which is passed as a query to <code>getIntradayData()</code>.</p>
  <p><code>getIntradayData()</code> takes all the parameters, fetches the data from the API and updates the answer in a global list called <code>listfinal</code>, which you can see is getting called in the last <code>for</code> loop for writing the data into the excel.</p>
  <p>A new excel sheet is formed like this</p>
  <pre><code>workbook = xlsxwriter.Workbook('tradingviewdata.xlsx')
  worksheet = workbook.add_worksheet(text)
  </code></pre>
  <p>Where <code>tradingviewdata.xlsx</code> is the name of the excel file and <code>text</code> is the sheet name.</p>
  <p>I want that whenever <code>getCSV()</code> is called it checks that if a sheet with the same <code>text</code> name also exists, if yes then it should update the new entries into that sheet. Else if that <code>text</code> name sheet does not exist it creates a new sheet and enters the values into that.</p>
  <p>I am calling the <code>getCSV()</code> inside a <code>tkinter</code> GUI like this</p>
  <pre><code>self.button1 = tk.Button(self, text=&quot;Submit&quot;, command=self.ok2)
  </code></pre>
  <pre><code>def ok2(self):
          self.interval=str(self.variable_a.get())
          self.shorttrend=str(self.variable_b.get())
          self.longtrend=str(self.variable_c.get())
          self.mic=str(self.variable_d.get())
          symbols=getTickers(self.mic)
          self.getCSV(symbols)
          print(&quot;done&quot;)
  </code></pre>
  <p>As for the problem, I found this related question's solution like this.</p>
  <pre><code>import xlsxwriter
  list_name = [&quot;first sheet&quot;, &quot;second sheet&quot;, &quot;third sheet&quot;]
  
  workbook = xlsxwriter.Workbook(&lt;Your full path&gt;)
  for sheet_name in list_name:
      worksheet = workbook.add_worksheet(sheet_name)
      worksheet.write('A1', sheet_name)
  
  workbook.close()
  </code></pre>
  <p>But I can't think of how to use this in my case as I don't have a ready-made list but it should create a new sheet whenever the function is called.</p>