A quick and dirty class for working with Excel via COM in Python. By far, not all of the power of Excel is available here, but it's a good start for simple tasks.
1
2 from win32com.client import constants, Dispatch
3 import pythoncom
4 import os
5
6 borderTop = 3
7 borderBottom = 4
8 borderLeft = 1
9 borderRight = 2
10 borderSolid = 1
11 borderDashed = 2
12 borderDotted = 3
13 colorBlack = 1
14 directionUp = -4162
15 directionDown = -4121
16 directionLeft = -4131
17 directionRight = -4152
18
19 class ExcelDocument(object):
20 """
21 Some convenience methods for Excel documents accessed
22 through COM.
23 """
24
25 def __init__(self, visible=False):
26 self.app = Dispatch("Excel.Application")
27 self.app.Visible = visible
28 self.sheet = 1
29
30 def new(self, filename=None):
31 """
32 Create a new Excel workbook. If 'filename' specified,
33 use the file as a template.
34 """
35 self.app.Workbooks.Add(filename)
36
37 def open(self, filename):
38 """
39 Open an existing Excel workbook for editing.
40 """
41 self.app.Workbooks.Open(filename)
42
43 def set_sheet(self, sheet):
44 """
45 Set the active worksheet.
46 """
47 self.sheet = sheet
48
49 def get_range(self, range):
50 """
51 Get a range object for the specified range or single cell.
52 """
53 return self.app.ActiveWorkbook.Sheets(self.sheet).Range(range)
54
55 def set_value(self, cell, value=''):
56 """
57 Set the value of 'cell' to 'value'.
58 """
59 self.get_range(cell).Value = value
60
61 def get_value(self, cell):
62 """
63 Get the value of 'cell'.
64 """
65 value = self.get_range(cell).Value
66 if isinstance(value, tuple):
67 value = [v[0] for v in value]
68 return value
69
70 def set_border(self, range, side, line_style=borderSolid, color=colorBlack):
71 """
72 Set a border on the specified range of cells or single cell.
73 'range' = range of cells or single cell
74 'side' = one of borderTop, borderBottom, borderLeft, borderRight
75 'line_style' = one of borderSolid, borderDashed, borderDotted, others?
76 'color' = one of colorBlack, others?
77 """
78 range = self.get_range(range).Borders(side)
79 range.LineStyle = line_style
80 range.Color = color
81
82 def sort(self, range, key_cell):
83 """
84 Sort the specified 'range' of the activeworksheet by the
85 specified 'key_cell'.
86 """
87 range.Sort(Key1=self.get_range(key_cell), Order1=1, Header=0, OrderCustom=1, MatchCase=False, Orientation=1)
88
89 def hide_row(self, row, hide=True):
90 """
91 Hide the specified 'row'.
92 Specify hide=False to show the row.
93 """
94 self.get_range('a%s' % row).EntireRow.Hidden = hide
95
96 def hide_column(self, column, hide=True):
97 """
98 Hide the specified 'column'.
99 Specify hide=False to show the column.
100 """
101 self.get_range('%s1' % column).EntireColumn.Hidden = hide
102
103 def delete_row(self, row, shift=directionUp):
104 """
105 Delete the entire 'row'.
106 """
107 self.get_range('a%s' % row).EntireRow.Delete(Shift=shift)
108
109 def delete_column(self, column, shift=directionLeft):
110 """
111 Delete the entire 'column'.
112 """
113 self.get_range('%s1' % column).EntireColumn.Delete(Shift=shift)
114
115 def fit_column(self, column):
116 """
117 Resize the specified 'column' to fit all its contents.
118 """
119 self.get_range('%s1' % column).EntireColumn.AutoFit()
120
121 def save(self):
122 """
123 Save the active workbook.
124 """
125 self.app.ActiveWorkbook.Save()
126
127 def save_as(self, filename, delete_existing=False):
128 """
129 Save the active workbook as a different filename.
130 If 'delete_existing' is specified and the file already
131 exists, it will be deleted before saving.
132 """
133 if delete_existing and os.path.exists(filename):
134 os.remove(filename)
135 self.app.ActiveWorkbook.SaveAs(filename)
136
137 def print_out(self):
138 """
139 Print the active workbook.
140 """
141 self.app.Application.PrintOut()
142
143 def close(self):
144 """
145 Close the active workbook.
146 """
147 self.app.ActiveWorkbook.Close()
148
149 def quit(self):
150 """
151 Quit Excel.
152 """
153 return self.app.Quit()