title: Convert excel to XML in python
body: <p>I am trying to convert excel database into python.
  I have a trading data which I need to import into the system in xml format.</p>
  <p>my code is following:</p>
  <pre><code>df = pd.read_excel(&quot;C:/Users/junag/Documents/XML/Portfolio2.xlsx&quot;, sheet_name=&quot;Sheet1&quot;, dtype=object)
  root = ET.Element('trading-data')
  root.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')
  tree = ET.ElementTree(root)
  Portfolios = ET.SubElement(root, &quot;Portfolios&quot;)
  Defaults = ET.SubElement(Portfolios, &quot;Defaults&quot;, BaseCurrency=&quot;USD&quot;)
  
  for row in df.itertuples():
  Portfolio = ET.SubElement(Portfolios, &quot;Portfolio&quot;, Name=row.Name, BaseCurrency=row.BaseCurrency2, TradingPower=str(row.TradingPower),
                            ValidationProfile=row.ValidationProfile, CommissionProfile=row.CommissionProfile)
  PortfolioPositions = ET.SubElement(Portfolio, &quot;PortfolioPositions&quot;)
  if row.Type == &quot;Cash&quot;:
      PortfolioPosition = ET.SubElement(PortfolioPositions, &quot;PortfolioPosition&quot;, Type=row.Type, Volume=str(row.Volume))
      Cash = ET.SubElement(PortfolioPosition, 'Cash', Currency=str(row.Currency))
  else:
      PortfolioPosition = ET.SubElement(PortfolioPositions, &quot;PortfolioPosition&quot;, Type=row.Type, Volume=str(row.Volume),
                                        Invested=str(row.Invested), BaseInvested=str(row.BaseInvested))
      Instrument = ET.SubElement(PortfolioPosition, 'Instrument', Ticker=str(row.Ticker), ISIN=str(row.ISIN), Market=str(row.Market),
                                 Currency=str(row.Currency2), CFI=str(row.CFI))
  
  
  ET.indent(tree, space=&quot;\t&quot;, level=0)
  tree.write(&quot;Portfolios_converted2.xml&quot;, encoding=&quot;utf-8&quot;)
  </code></pre>
  <p>The output looks like this:
  <a href="https://i.stack.imgur.com/wDnI8.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>While I need it to look like this:
  <a href="https://i.stack.imgur.com/ee3YF.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>How can I improve my code to make the output xml look better? please advise</p>
  <p>here the excel data:
  <a href="https://i.stack.imgur.com/TlR8G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TlR8G.png" alt="enter image description here" /></a></p>