defunpack_rar(filedir): for filename in os.listdir(filedir): if filename.endswith('.rar'): rar_path = os.path.join(filedir, filename) unpack_dir = rar_path.split('.')[0] ifnot os.path.exists(unpack_dir): os.mkdir(unpack_dir) # x exclude file, -o output dir cmd = f'7z.exe x {rar_path} -o{unpack_dir}' result = os.popen(cmd).read() if'Everything is Ok'in result: print(f'unpack {filename} success')
defget_xls_files(filedir): files_list = [] for filename in os.listdir(filedir): filepath = os.path.join(filedir, filename) if os.path.isdir(filepath): for filename in os.listdir(filepath): if filename.endswith(('.xls', 'xlsx')): files_list.append(os.path.join(filepath, filename)) return files_list
defparse_data(filename): data = xlrd.open_workbook(filename) tables = data.sheets() # get all sheets list_values = [] for table in tables: nrows = table.nrows # rows ncols = table.ncols # cols start = 1# start col, except titile end = nrows # end col
for x inrange(start, end): values = [] row = table.row_values(x) for i inrange(0, ncols): values.append(str(row[i])) # database all fields use varchar2 values.append(filename.split('\\')[-2]) # use dir as sheetname list_values.append(values) return list_values
defimport_db(data_rows):
con = cx_Oracle.connect(conn_str)
# Create Oracle DB Cursor cursor = cx_Oracle.Cursor(con)
# Insert data to table # print(con.version)
# for row in data_rows: # # get seq # seq = cursor.execute('select som_edi_seq_id_s.nextval from dual') # # seq.fetchone() is a tuple # row.insert(0, seq.fetchone()[0])
defread_sheet(sheet): """Read a worksheet's content into a 2-dimensional list :sheet: xlrd.sheet.Sheet instance :returns: list """ sheet_values = [] for row_num inrange(sheet.nrows): row = sheet.row(row_num) row_values = [] for cell in row: if cell.ctype == 0: # XL_CELL_EMPTY row_values.append(None) elif cell.ctype == 2: # XL_CELL_NUMBER # Trim the trailing ".0" by converting the number to int. if cell.value == int(cell.value): row_values.append(int(cell.value)) else: row_values.append(cell.value) elif cell.ctype == 3: # XL_CELL_DATE # Convert an Excel date/time number into a datetime.datetime object. # datemode=0: 1900-based on Windows # if excel only time show, row will show day 1899/12/31 # such as 18:45:05 => datetime.datetime(1899, 12, 31, 18, 45, 5) row_values.append( xlrd.xldate.xldate_as_datetime(cell.value, datemode=0)) elif cell.ctype == 4: # XL_CELL_BOOLEAN row_values.append(bool(cell.value)) elif cell.ctype == 5: # XL_CELL_ERROR row_values.append(xlrd.biffh.error_text_from_code[cell.value]) else: row_values.append(cell.value) sheet_values.append(row_values) return sheet_values
if __name__ == "__main__": data = xlrd.open_workbook('test_excel.xlsx') tables = data.sheets() # get all sheets for table in tables: data = read_sheet(table) pprint(data)
# create or replace procedure edi_use_ref_cursor(p_in varchar2, # p_out_cur out sys_refcursor) as # begin # if p_in = 'ENAME' then # open p_out_cur for # select ename from scott.emp; # elsif p_in = 'DNAME' then # open p_out_cur for # select dname from scott.dept; # else # open p_out_cur for # select 'Invalid Input' from dual; # end if; # end;
import cx_Oracle
conn = cx_Oracle.connect('username/password@instance') cur = conn.cursor() refcursor = conn.cursor()