-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_funcs.py
More file actions
70 lines (51 loc) · 1.99 KB
/
core_funcs.py
File metadata and controls
70 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import win32com.client
import pandas as pd
import pyodbc
import warnings
class ExcelData():
"""
class to load different datasource to excel
"""
def __init__(self, df=None):
self.df = df
def df_to_curr_xl(self, ):
# Create an instance of the Excel Application & make it visible.
ExcelApp = win32com.client.GetActiveObject("Excel.Application")
ExcelApp.Visible = True
rows, cols = self.df.shape
headers = self.df.columns.tolist()
# Take the data frame object and convert it to a recordset array
rec_array = self.df.to_records(index=False)
# Convert the Recordset Array to a list.
# This is because Excel doesn't recognize Numpy datatypes.
rec_array = rec_array.tolist()
# set the value property equal to the record array.
header_start_rng = ExcelApp.ActiveCell.Address
header_end_rng = ExcelApp.ActiveSheet.Range(
header_start_rng).resize(1, cols).Address
header_output_rng = f'{header_start_rng}:{header_end_rng}'
start_rng = ExcelApp.ActiveSheet.Range(
header_start_rng).resize(2, 1).Address
end_rng = ExcelApp.ActiveSheet.Range(
start_rng).resize(rows, cols).Address
output_rng = f'{start_rng}:{end_rng}'
ExcelApp.ActiveSheet.Range(header_output_rng).Value = headers
ExcelApp.ActiveSheet.Range(output_rng).Value = rec_array
def load_data_file(self, file_path):
self.df = pd.read_csv(file_path)
return 0
def load_data_sql(self, sql_path, server='WENJING-DESKTOP\\SQLEXPRESS'):
conn_str = f'''
Driver={{SQL Server}};
Server={server};
Trusted_Connection=yes;
'''
warnings.filterwarnings('ignore')
conn = pyodbc.connect(conn_str)
with open(sql_path, 'r') as query:
self.df = pd.read_sql_query(query.read(), conn)
return 0
def main():
pass
if __name__ == '__main__':
main()