title: How to write pivot table values (columns) from pandas to excel using openpyxl?
body: <p>I can write values to an existing excel worksheet but I am unable to export values from pivot table on pandas to excel sheet using openpyxl.
  Below are my code and what I am capable:</p>
  
  <pre><code>import pandas as pd
  import openpyxl as op
  import numpy as np
  from openpyxl import Workbook, worksheet, load_workbook
  
  wb = op.load_workbook("Table1.xlsx")
  #ws = wb.active # selects active excel sheet
  
  print(wb.sheetnames) # Shows all available sheet names
  
  ws = wb['Sheet1'] # Select sheet name "Sheet1"
  ws['B2'] = 40 # Input on cell B2
  ws['B3'] = 18
  ws['B4'] = 20
  ws['B5'] = 20
  ws['B6'] = 20
  ws['C2'] = 8 # Input on cell C2
  ws['C3'] = 30
  ws['C4'] = 4
  ws['C5'] = 10
  ws['C6'] = 9
  ws['D2'] = 89 # Input on cell D2
  ws['D3'] = 300
  ws['D4'] = 76
  ws['D5'] = 20
  ws['D6'] = 4
  
  ws1 = wb['agua'] # Select sheet name "agua"
  ws1['B2'] = 4 # Input on cell B2
  ws1['B3'] = 60
  ws1['B4'] = 0
  ws1['C2'] = 90
  ws1['C3'] = 23
  ws1['C4'] = 20
  
  wb.save("test.xlsx") # Saves to new excell worksheet to avoid mistakes
  </code></pre>
  
  <p>But I have this pivot table output that I need to fill each column of pivot table to that existing excel file sheet to be filled automatically. Look below:</p>
  
  <pre><code>df2 = pd.read_csv("https://www.dropbox.com/s/90y07129zn351z9/test_data.csv?dl=1",encoding="latin-1")
  
  df2['received'] = pd.to_datetime(df2['received'])
  df2['sent'] = pd.to_datetime(df2['sent'])
  
  pvt_all = df2.dropna(axis=0, how='all', subset=['received', 'sent'])\
      .pivot_table(index=['site'], values=['received','sent'],\
      aggfunc='count', margins=True, dropna=False)
  pvt_all['to_send']= pvt_all['received']-pvt_all['sent'] 
  pvt_all=pvt_all[['received','sent','to_send']] 
  pvt_all
  
  received    sent    to_send
  site            
  2   32.0    27.0    5.0
  3   20.0    17.0    3.0
  4   33.0    31.0    2.0
  5   40.0    31.0    9.0
  All 125.0   106.0   19.0
  </code></pre>
  
  <p>Full dataset is in the link, I cant share (post) here because stackoverflow.com limits characters to 30000</p>
  
  <p>What I want is to write these column values below:</p>
  
  <pre><code>received    sent    to_send
  site            
  2   32.0    27.0    5.0
  3   20.0    17.0    3.0
  4   33.0    31.0    2.0
  5   40.0    31.0    9.0
  All 125.0   106.0   19.0
  </code></pre>
  
  <p>To an existing excel workbook already with headers and index like below:</p>
  
  <pre><code>received    sent    to_send
  site            
  2       
  3       
  4   
  5   
  All 
  </code></pre>
  
  <p>I have more features for excel sheet but I just want to understand how to code to achieve desired result.</p>