-
Notifications
You must be signed in to change notification settings - Fork 4
/
xlwings_utilities.py
85 lines (65 loc) · 2.29 KB
/
xlwings_utilities.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Wrapper functions for xlwings
"""
import pandas as pd
from xlwings import Workbook, Sheet, Range
# Utililty functions for dataframes
def clear(range_name, ws=None):
"""Clear a table started in a range cell, ex: clear('a1').
clear(range_name, ws=None)
"""
ws = Sheet.active() if ws is None else ws
Range(ws.name, range_name).table.clear()
def clear_ws(ws=None):
"""Clear a table started in a range cell, ex: clearcontents('a1').
clear_ws(ws=None)
"""
ws = Sheet.active() if ws is None else ws
ws.clear()
def clearcontents(range_name, ws=None):
"""Clear a table started in a range cell, ex: clearcontents('a1').
clearcontents(range_name, ws=None)
"""
ws = Sheet.active() if ws is None else ws
Range(ws.name, range_name).table.clear_contents()
def clearcontents_ws(ws=None):
"""Clear entire worksheet, ex: clearcontents_ws(ws=worksheetobj).
clearcontents_ws(ws=None)
"""
ws = Sheet.active() if ws is None else ws
ws.clear_contents()
def list_write(range_name, value_list, ws=None):
"""Write a list vertially.
list_write(range_name, value_list, ws=None)
"""
ws = Sheet.active() if ws is None else ws
# Turn list into a column
value_column = [[e] for e in value_list]
Range(ws.name, range_name).value = value_column
def list_read(range_name, ws=None):
"""Read a list vertially.
list_read(range_name, value_list, ws=None)
"""
ws = Sheet.active() if ws is None else ws
# Range(range_name).options(transpose=True).value = value_list
datalist = Range(ws.name, range_name).vertical.value
return datalist
def df_read(range_name, ws=None):
"""Return dataframe from range name and Sheet.; ex: df = df_read('a1').
df_read(range_name, ws=None)
"""
ws = Sheet.active() if ws is None else ws
data = Range(ws.name, range_name).table.value
df = pd.DataFrame(data[1:], columns=data[0])
return df
def df_write(df, range_name, ws=None):
"""Write a dataframe to a cell.
df_write(df, range_name, ws=None)
"""
ws = Sheet.active() if ws is None else ws
Range(ws.name, range_name, index=False).value = df # without pd indices
def autofit(ws=None):
"""Autofit columns of worksheet
"""
ws = Sheet.active() if ws is None else ws
ws.autofit(axis='columns')