title: Using PasteExcelTable to copy Excel data to the body of an Outlook email with Python
body: <p><a href="https://i.stack.imgur.com/UbQZQ.png" rel="nofollow noreferrer">Formatted Excel range copied to a Word file</a></p>
  
  <p>This copies a range of cells from Excel and pastes them into a Word document with formatting preserved. The code works for this. However, I also want to paste the data into the body of an email with the cell styles.</p>
  
  <pre class="lang-py prettyprint-override"><code>import sys
  from pathlib import Path
  import win32com.client as win32
  
  
  excel_path = str(Path.cwd() / 'input.xlsx')
  excel = win32.gencache.EnsureDispatch('Excel.Application')
  excel.Visible = False
  excel.DisplayAlerts = False
  wb = excel.Workbooks.Open(excel_path)
  ws = wb.Worksheets(1)
  ws.Range("A1:B2").Copy()
  wb.Close()
  
  word_path = str(Path.cwd() / 'output.docx')
  word = win32.gencache.EnsureDispatch('Word.Application')
  doc = word.Documents.Open(word_path)
  doc.Content.PasteExcelTable(False, False, True)
  doc.Close()
  
  # That works, but now I want to paste the copied Excel range into the body of
  # an email. The solution may look something like the following with the "X"
  # as a "Selection" or "Range" object for the PasteExcelTable method.
  
  outlook = win32.gencache.EnsureDispatch('Outlook.Application')
  new_mail = outlook.CreateItem(0)
  new_mail.To = 'person@email.com'
  
  new_mail.Body = ws.Range("A1:B2").PasteExcelTable(False, False, True)
  
  new_mail.Send()
  sys.exit()
  </code></pre>
  
  <p>I believe <strong>[PasteExcelTable]</strong><a href="https://docs.microsoft.com/en-us/office/vba/api/word.range.pasteexceltable" rel="nofollow noreferrer">2</a> can be used to make this happen with Outlook as it does for Word files and perhaps someone knows how this can be done.</p>
  
  <p>I'm not defining the range correctly. Here's an example of one of my attempts:</p>
  
  <pre class="lang-py prettyprint-override"><code>    new_mail.Body = ws.Range("A1:B2").PasteExcelTable(False, False, True)
  </code></pre>
  
  <p>I know this could be accomplished with VBA, but I want to use Python and to see whether PasteExcelTable will work for this. If it is not possible, I will capture an image of the data range and paste that to the email.</p>