WIN32COM saving/exporting multiple sheets as PDF
<p>I'd like to select multiple sheets (in a certain order) in a workbook and export them as a single PDF using win32com. I tried:</p>

<pre><code>wb_HC.Sheets(['sheetname1','sheetname2']).ExportAsFixedFormat(0, workdir+'\\'+run_date+'_MD_Summary.pdf')
</code></pre>

<p>But this does not work. I know that in VBA sheets can be called as an array of sheets:</p>

<pre><code>Array(sheets(1), sheets(2))
</code></pre>

<p>But I think this is not equivalent to list() in Python.</p>

<p>Consider using Python's list <code>[]</code> method which could correspond to VBA's <code>Array()</code>. Apply it to the <code>Worksheets()</code> method passing either Sheet index number or Sheet name. Then in the <code>ExportAsFixedFormat</code> qualify the method with <code>xlApp.ActiveSheet</code>:</p>

<pre><code>import os
import win32com.client

workdir = "C:\\Path\\To\\Working\\Directory"
rundate = '2016-09-11'

try:
    xlApp = win32com.client.Dispatch("Excel.Application")
    wb = xlApp.Workbooks.Open(os.path.join(workdir, 'ExcelFile.xlsx'))

    wb.Worksheets(["Sheet1", "Sheet2"]).Select()

    xlTypePDF = 0
    xlQualityStandard = 0

    xlApp.ActiveSheet.ExportAsFixedFormat(xlTypePDF, 
                                          os.path.join(workdir, run_date+'_MD_Summary.pdf'), 
                                          xlQualityStandard, True, True)    
except Exception as e:
    print(e)

finally:    
    wb.Close(False)
    xlApp.Quit

    wb = None
    xlApp = None
</code></pre>

<p>By the way, be sure to wrap your processing in <code>try/except/finally</code> blocks. Otherwise, any error you encounter will leave the Excel.exe running in background process. In this setup, the script always closes workbook and uninitializes the COM object regardless of error. The counterpart in VBA would be the <code>On Error Goto ...</code> handling (another best practice method).</p>

python excel win32com