python pandas retrieve data from the excel

import xlrd
from xlrd import xldate_as_tuple
from datetime import datetime
import pandas as pd
from sqlalchemy import create_engine


def ordfun(zimu):
return ord(zimu)-65
def realnum(shuzi):
return shuzi-1

def getDate(sheet,row,column):
cell = sheet.cell_value(realnum(row), ordfun(column))
val = datetime(*xldate_as_tuple(cell, 0)).strftime('%Y/%m/%d') # '%Y/%m/%d %H:%M:%S'
return val

def nvl(input):
if input is None or input=='':
return 0
else:
return int(eval(str(input).replace('=','')))

def intoDB(df,tableName):
= create_engine db_engine ( '+ PostgreSQL psycopg2: //' + 'gpadmin' + ':' + 'gpadmin' + '@' + STR ('10 .39.4.175 ') +' / '+' pissh ')
pd.io. sql.to_sql (DF, tableName, db_engine, if_exists = 'the append', index = False)

# database connection is obtained
DEF getEngine ():
db_engine create_engine = ( '+ PostgreSQL psycopg2: //' + 'gpadmin' + ':' + 'gpadmin' + '@' + str ('10 .39.4.175 ') +' / '+' pissh ')
return db_engine


wb = xlrd.open_workbook (' E: // Guangdong air conditioning / PISSH total _20190730 / P1_2 times _CU1 line (a handling) 2.xlsx ')
# empty list a statement list
the listAll = []

# through all Sheet
for I in Range (1,32):
Sheet = wb.sheet_by_name (STR (I))
checkcellvalue = Sheet. cell (realnum (28), ordfun ( 'N '.)) Value
#Print (' sheetname: R & lt%, check value:% r '% (str ( i), checkcellvalue))
if checkcellvalue is None or checkcellvalue=='':
continue
try:
dict = {'cal_date': getDate(sheet,5,'C')
, 'project_name': sheet.cell(realnum(5),ordfun('F')).value
, 'class': sheet.cell(realnum(5),ordfun('H')).value
, 'plan_num': sheet.cell(realnum(28),ordfun('N')).value
, 'real_num': sheet.cell(realnum(29),ordfun('N')).value
, 'overtime_plan_num': nvl(sheet.cell(realnum(28),ordfun('Q')).value)
, 'overtime_real_num': nvl(sheet.cell(realnum(29),ordfun('Q')).value)}
except:
"从sheet生成字典出错,错误地点为:%r"% sheet.name

listAll.append(dict)

df = pd.DataFrame(listAll)
= cols [ 'cal_date', 'class', 'project_name', 'plan_num', 'real_num', 'overtime_plan_num', 'overtime_real_num']
DF = df.ix [:, cols] # sequentially outputs the dataframe

# engine obtained
= getEngine Engine ()

allDict = df.to_dict (Orient = 'Records')
for Row in allDict:
Print (tuple (row.values ()))
Table = ' "PAPAGZ_KPI_COMPREHENSIVE_RATE_DATA"'
# key acquired in a comma-separated and string, returns a string
Keys = ','. the Join (row.keys ())
values = ','. the Join ([ '% S'] * len (Row))
SQL = 'the INSERT the INTO Table {} ({} Keys) the vALUES values {} 'the format (Table = Table, Keys Keys =, = tuple values (row.values ())).
the try:
pd.io.sql.execute (SQL,Engine)
the except Exception AS E:
Print ( "exception is caught, exp =", e)

Guess you like

Origin www.cnblogs.com/zhangwensi/p/11290352.html