<s> """<STR_LIT>""" <EOL> from decorator import decorator <EOL> import numpy as np <EOL> import pywintypes <EOL> import re <EOL> try : <EOL> import win32com . client <EOL> _com_available = True <EOL> except ImportError : <EOL> _com_available = False <EOL> @ decorator <EOL> def needs_com ( func , * args , ** kwargs ) : <EOL> if not _com_available : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return func ( * args , ** kwargs ) <EOL> def _to_range ( col , row ) : <EOL> """<STR_LIT>""" <EOL> cell = "<STR_LIT>" <EOL> while col >= <NUM_LIT> : <EOL> cell = "<STR_LIT>" % ( chr ( ord ( "<STR_LIT:A>" ) + ( col % <NUM_LIT> ) ) , cell ) <EOL> col = ( col // <NUM_LIT> ) - <NUM_LIT:1> <EOL> cell = "<STR_LIT>" % ( chr ( ord ( "<STR_LIT:A>" ) + ( col % <NUM_LIT> ) ) , cell ) <EOL> cell += "<STR_LIT>" % ( row + <NUM_LIT:1> ) <EOL> return cell <EOL> def _to_numeric_range ( cell ) : <EOL> """<STR_LIT>""" <EOL> match = re . match ( "<STR_LIT>" , cell . upper ( ) ) <EOL> if not match : <EOL> raise RuntimeError ( "<STR_LIT>" % cell ) <EOL> col , row = match . groups ( ) <EOL> col_digits = map ( lambda c : ord ( c ) - ord ( "<STR_LIT:A>" ) + <NUM_LIT:1> , col ) <EOL> col = <NUM_LIT:0> <EOL> for digit in col_digits : <EOL> col = ( col * <NUM_LIT> ) + digit <EOL> row = int ( row ) - <NUM_LIT:1> <EOL> col = col - <NUM_LIT:1> <EOL> return col , row <EOL> def _get_book_and_sheet ( app , sheet_name ) : <EOL> """<STR_LIT>""" <EOL> match = re . match ( "<STR_LIT>" , sheet_name ) <EOL> if not match : <EOL> raise AssertionError ( "<STR_LIT>" % sheet_name ) <EOL> workbook_name , sheet_name = match . groups ( ) <EOL> if workbook_name is None : <EOL> wb = app . ActiveWorkbook <EOL> else : <EOL> try : <EOL> wb = app . Workbooks ( workbook_name ) <EOL> except pywintypes . com_error : <EOL> raise RuntimeError ( "<STR_LIT>" % workbook_name ) <EOL> try : <EOL> sheet = wb . Sheets ( sheet_name ) <EOL> except pywintypes . com_error : <EOL> raise RuntimeError ( "<STR_LIT>" % sheet_name ) <EOL> return wb , sheet <EOL> @ needs_com <EOL> def export_dataframe ( dataframe , <EOL> sheet_name = None , <EOL> target_cell = "<STR_LIT>" , <EOL> use_active_workbook = False , <EOL> use_active_sheet = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( dataframe , list ) : <EOL> dataframes = dataframe <EOL> else : <EOL> dataframes = [ dataframe ] <EOL> app = win32com . client . GetActiveObject ( "<STR_LIT>" ) <EOL> app = win32com . client . gencache . EnsureDispatch ( app ) <EOL> orig_calc_mode = app . Calculation <EOL> orig_updating = app . ScreenUpdating <EOL> try : <EOL> app . ScreenUpdating = False <EOL> app . Calculation = win32com . client . constants . xlCalculationManual <EOL> if target_cell == "<STR_LIT>" : <EOL> assert sheet_name is None , "<STR_LIT>" <EOL> use_active_sheet = True <EOL> target_cell = app . ActiveCell . GetAddress ( ) <EOL> if sheet_name is not None : <EOL> wb , sheet = _get_book_and_sheet ( app , sheet_name ) <EOL> next_sheet_index = wb . Sheets . Count + <NUM_LIT:1> <EOL> elif use_active_sheet : <EOL> wb = app . ActiveWorkbook <EOL> sheet = app . ActiveSheet <EOL> next_sheet_index = wb . Sheets . Count + <NUM_LIT:1> <EOL> elif use_active_workbook : <EOL> wb = app . ActiveWorkbook <EOL> sheet = wb . Sheets . Add ( ) <EOL> next_sheet_index = wb . Sheets . Count + <NUM_LIT:1> <EOL> else : <EOL> wb = app . Workbooks . Add ( ) <EOL> sheet = wb . Sheets ( <NUM_LIT:1> ) <EOL> next_sheet_index = <NUM_LIT:2> <EOL> top_left_col , top_left_row = _to_numeric_range ( target_cell ) <EOL> def _format ( x ) : <EOL> if isinstance ( x , float ) : <EOL> if np . isnan ( x ) or np . isinf ( x ) : <EOL> return "<STR_LIT>" <EOL> if isinstance ( x , ( tuple , list ) ) : <EOL> return repr ( x ) <EOL> return x <EOL> while len ( dataframes ) > <NUM_LIT:0> : <EOL> dataframe = dataframes . pop ( <NUM_LIT:0> ) <EOL> sheet . Range ( _to_range ( top_left_col , top_left_row ) ) . Value = "<STR_LIT>" <EOL> range = sheet . Range ( _to_range ( top_left_col , top_left_row + <NUM_LIT:1> ) , <EOL> _to_range ( top_left_col , top_left_row + len ( dataframe . index ) ) ) <EOL> range . Value = [ [ _format ( x ) ] for x in dataframe . index . tolist ( ) ] <EOL> for i , col in enumerate ( dataframe . columns ) : <EOL> col_num = top_left_col + i + <NUM_LIT:1> <EOL> sheet . Range ( _to_range ( col_num , top_left_row ) ) . Value = col <EOL> range = sheet . Range ( _to_range ( col_num , top_left_row + <NUM_LIT:1> ) , <EOL> _to_range ( col_num , top_left_row + len ( dataframe . index ) ) ) <EOL> range . Value = [ [ _format ( x ) ] for x in dataframe [ col ] . tolist ( ) ] <EOL> if len ( dataframes ) > <NUM_LIT:0> : <EOL> if next_sheet_index > wb . Sheets . Count : <EOL> sheet = wb . Sheets . Add ( After = sheet ) <EOL> next_sheet_index = wb . Sheets . Count + <NUM_LIT:1> <EOL> else : <EOL> sheet = wb . Sheets . Item ( next_sheet_index ) <EOL> next_sheet_index += <NUM_LIT:1> <EOL> finally : <EOL> app . ScreenUpdating = orig_updating <EOL> app . Calculation = orig_calc_mode <EOL> del app </s>
