Skip to content

Commit 13b88d2

Browse files
committed
Aspose.Cells Java for Python - Example
1 parent 8d625c0 commit 13b88d2

File tree

116 files changed

+10863
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+10863
-0
lines changed

Plugins/Aspose-Cells-Java-for-Python/WorkingWithFiles/__init__.py

Lines changed: 494 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 397 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,397 @@
1+
__author__ = 'fahadadeel'
2+
import jpype
3+
4+
class RowsAndColumns:
5+
6+
def __init__(self,dataDir):
7+
8+
self.dataDir = dataDir
9+
self.Workbook = jpype.JClass("com.aspose.cells.Workbook")
10+
11+
def main(self):
12+
13+
# Inserting a Row
14+
self.insert_row()
15+
16+
# Inserting Multiple Rows
17+
self.insert_multiple_rows()
18+
19+
# Deleting a Row
20+
self.delete_row()
21+
22+
# Deleting Multiple Rows
23+
self.delete_multiple_rows()
24+
25+
# Inseting one or Multiple Columns
26+
self.insert_column()
27+
28+
# Deleting a Column
29+
self.delete_column()
30+
31+
# Hiding Rows and Columns
32+
self.hide_rows_columns()
33+
34+
# Showing Rows and Columns
35+
self.unhide_rows_columns()
36+
37+
# Grouping Rows & Columns
38+
self.group_rows_columns()
39+
40+
# Ungrouping Rows & Columns
41+
self.ungroup_rows_columns()
42+
43+
# Setting the Row Height
44+
self.set_row_height()
45+
46+
# Setting the Width of a Column
47+
self.set_column_width()
48+
49+
# Auto Fit Row
50+
self.autofit_row()
51+
52+
# Auto Fit Column
53+
self.autofit_column()
54+
55+
# Copying Rows
56+
self.copy_rows()
57+
58+
# Copying Columns
59+
self.copy_columns()
60+
61+
def insert_row(self):
62+
63+
# Instantiating a Workbook object by excel file path
64+
workbook = self.Workbook(self.dataDir + "Book1.xls")
65+
66+
# Accessing the first worksheet in the Excel file
67+
worksheet = workbook.getWorksheets().get(0)
68+
69+
# Inserting a row into the worksheet at 3rd position
70+
worksheet.getCells().insertRows(2,1)
71+
72+
# Saving the modified Excel file in default (that is Excel 2003) format
73+
workbook.save(self.dataDir + "Insert Row.xls")
74+
75+
print "Insert Row Successfully."
76+
77+
78+
79+
def insert_multiple_rows(self):
80+
81+
# Instantiating a Workbook object by excel file path
82+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
83+
84+
# Accessing the first worksheet in the Excel file
85+
worksheet = workbook.getWorksheets().get(0)
86+
87+
# Inserting a row into the worksheet at 3rd position
88+
worksheet.getCells().insertRows(2,10)
89+
90+
# Saving the modified Excel file in default (that is Excel 2003) format
91+
workbook.save(self.dataDir + "Insert Multiple Rows.xls")
92+
93+
print "Insert Multiple Rows Successfully."
94+
95+
96+
97+
def delete_row(self):
98+
99+
# Instantiating a Workbook object by excel file path
100+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
101+
102+
# Accessing the first worksheet in the Excel file
103+
worksheet = workbook.getWorksheets().get(0)
104+
105+
# Deleting 3rd row from the worksheet
106+
worksheet.getCells().deleteRows(2,1,True)
107+
108+
# Saving the modified Excel file in default (that is Excel 2003) format
109+
workbook.save(self.dataDir + "Delete Row.xls")
110+
111+
print "Delete Row Successfully."
112+
113+
114+
115+
def delete_multiple_rows(self):
116+
117+
# Instantiating a Workbook object by excel file path
118+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
119+
120+
# Accessing the first worksheet in the Excel file
121+
worksheet = workbook.getWorksheets().get(0)
122+
123+
# Deleting 10 rows from the worksheet starting from 3rd row
124+
worksheet.getCells().deleteRows(2,10,True)
125+
126+
# Saving the modified Excel file in default (that is Excel 2003) format
127+
workbook.save(self.dataDir + "Delete Multiple Rows.xls")
128+
129+
print "Delete Multiple Rows Successfully."
130+
131+
132+
133+
def insert_column(self):
134+
135+
# Instantiating a Workbook object by excel file path
136+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
137+
138+
# Accessing the first worksheet in the Excel file
139+
worksheet = workbook.getWorksheets().get(0)
140+
141+
# Inserting a column into the worksheet at 2nd position
142+
worksheet.getCells().insertColumns(1,1)
143+
144+
# Saving the modified Excel file in default (that is Excel 2003) format
145+
workbook.save(self.dataDir + "Insert Column.xls")
146+
147+
print "Insert Column Successfully."
148+
149+
150+
151+
def delete_column(self):
152+
153+
# Instantiating a Workbook object by excel file path
154+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
155+
156+
# Accessing the first worksheet in the Excel file
157+
worksheet = workbook.getWorksheets().get(0)
158+
159+
# Deleting a column from the worksheet at 2nd position
160+
worksheet.getCells().deleteColumns(1,1,True)
161+
162+
# Saving the modified Excel file in default (that is Excel 2003) format
163+
workbook.save(self.dataDir + "Delete Column.xls")
164+
165+
print "Delete Column Successfully."
166+
167+
168+
169+
def hide_rows_columns(self):
170+
171+
# Instantiating a Workbook object by excel file path
172+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
173+
174+
# Accessing the first worksheet in the Excel file
175+
worksheet = workbook.getWorksheets().get(0)
176+
cells = worksheet.getCells()
177+
178+
# Hiding the 3rd row of the worksheet
179+
cells.hideRow(2)
180+
181+
# Hiding the 2nd column of the worksheet
182+
cells.hideColumn(1)
183+
184+
# Saving the modified Excel file in default (that is Excel 2003) format
185+
workbook.save(self.dataDir + "Hide Rows And Columns.xls")
186+
187+
print "Hide Rows And Columns Successfully."
188+
189+
190+
191+
def unhide_rows_columns(self):
192+
193+
# Instantiating a Workbook object by excel file path
194+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
195+
196+
# Accessing the first worksheet in the Excel file
197+
worksheet = workbook.getWorksheets().get(0)
198+
cells = worksheet.getCells()
199+
200+
# Unhiding the 3rd row and setting its height to 13.5
201+
cells.unhideRow(2,13.5)
202+
203+
# Unhiding the 2nd column and setting its width to 8.5
204+
cells.unhideColumn(1,8.5)
205+
206+
# Saving the modified Excel file in default (that is Excel 2003) format
207+
workbook.save(self.dataDir + "Unhide Rows And Columns.xls")
208+
209+
print "Unhide Rows And Columns Successfully."
210+
211+
212+
213+
def group_rows_columns(self):
214+
215+
# Instantiating a Workbook object by excel file path
216+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
217+
218+
# Accessing the first worksheet in the Excel file
219+
worksheet = workbook.getWorksheets().get(0)
220+
cells = worksheet.getCells()
221+
222+
# Grouping first six rows (from 0 to 5) and making them hidden by passing true
223+
cells.groupRows(0,5,True)
224+
225+
# Grouping first three columns (from 0 to 2) and making them hidden by passing true
226+
cells.groupColumns(0,2,True)
227+
228+
# Saving the modified Excel file in default (that is Excel 2003) format
229+
workbook.save(self.dataDir + "Group Rows And Columns.xls")
230+
231+
print "Group Rows And Columns Successfully."
232+
233+
234+
235+
def ungroup_rows_columns(self):
236+
237+
# Instantiating a Workbook object by excel file path
238+
workbook = self.Workbook(self.dataDir + 'Group Rows And Columns.xls')
239+
240+
# Accessing the first worksheet in the Excel file
241+
worksheet = workbook.getWorksheets().get(0)
242+
cells = worksheet.getCells()
243+
244+
# Ungrouping first six rows (from 0 to 5)
245+
cells.ungroupRows(0,5)
246+
247+
# Ungrouping first three columns (from 0 to 2)
248+
cells.ungroupColumns(0,2)
249+
250+
# Saving the modified Excel file in default (that is Excel 2003) format
251+
workbook.save(self.dataDir + "Ungroup Rows And Columns.xls")
252+
253+
print "Ungroup Rows And Columns Successfully."
254+
255+
256+
257+
def set_row_height(self):
258+
259+
# Instantiating a Workbook object by excel file path
260+
workbook = self.Workbook(self.self.dataDir + 'Book1.xls')
261+
262+
# Accessing the first worksheet in the Excel file
263+
worksheet = workbook.getWorksheets().get(0)
264+
cells = worksheet.getCells()
265+
266+
# Setting the height of the second row to 13
267+
cells.setRowHeight(1, 13)
268+
269+
# Saving the modified Excel file in default (that is Excel 2003) format
270+
workbook.save(self.dataDir + "Set Row Height.xls")
271+
272+
print "Set Row Height Successfully."
273+
274+
275+
276+
def set_column_width(self):
277+
278+
# Instantiating a Workbook object by excel file path
279+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
280+
281+
# Accessing the first worksheet in the Excel file
282+
worksheet = workbook.getWorksheets().get(0)
283+
cells = worksheet.getCells()
284+
285+
# Setting the width of the second column to 17.5
286+
cells.setColumnWidth(1, 17.5)
287+
288+
# Saving the modified Excel file in default (that is Excel 2003) format
289+
workbook.save(self.dataDir + "Set Column Width.xls")
290+
291+
print "Set Column Width Successfully."
292+
293+
294+
295+
def autofit_row(self):
296+
297+
# Instantiating a Workbook object by excel file path
298+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
299+
300+
# Accessing the first worksheet in the Excel file
301+
worksheet = workbook.getWorksheets().get(0)
302+
303+
# Auto-fitting the 3rd row of the worksheet
304+
worksheet.autoFitRow(2)
305+
306+
# Auto-fitting the 3rd row of the worksheet based on the contents in a range of
307+
# cells (from 1st to 9th column) within the row
308+
#worksheet.autoFitRow(2,0,8) # Uncomment this line if you to do AutoFit Row in a Range of Cells. Also, comment line 288.
309+
310+
# Saving the modified Excel file in default (that is Excel 2003) format
311+
workbook.save(self.dataDir + "Autofit Row.xls")
312+
313+
print "Autofit Row Successfully."
314+
315+
316+
317+
def autofit_column(self):
318+
319+
# Instantiating a Workbook object by excel file path
320+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
321+
322+
# Accessing the first worksheet in the Excel file
323+
worksheet = workbook.getWorksheets().get(0)
324+
325+
# Auto-fitting the 4th column of the worksheet
326+
worksheet.autoFitColumn(3)
327+
328+
# Auto-fitting the 4th column of the worksheet based on the contents in a range of
329+
# cells (from 1st to 9th row) within the column
330+
#worksheet.autoFitColumn(3,0,8) #Uncomment this line if you to do AutoFit Column in a Range of Cells. Also, comment line 310.
331+
332+
# Saving the modified Excel file in default (that is Excel 2003) format
333+
workbook.save(self.dataDir + "Autofit Column.xls")
334+
335+
print "Autofit Column Successfully."
336+
337+
338+
339+
def copy_rows(self):
340+
341+
# Instantiating a Workbook object by excel file path
342+
workbook = self.Workbook(self.dataDir + 'Book1.xls')
343+
344+
# Accessing the first worksheet in the Excel file
345+
worksheet = workbook.getWorksheets().get(0)
346+
347+
# Copy the second row with data, formattings, images and drawing objects
348+
# to the 12th row in the worksheet.
349+
worksheet.getCells().copyRow(worksheet.getCells(),1,11)
350+
351+
# Saving the modified Excel file in default (that is Excel 2003) format
352+
workbook.save(self.dataDir + "Copy Rows.xls")
353+
354+
print "Copy Rows Successfully."
355+
356+
357+
358+
def copy_columns(self):
359+
360+
# Instantiating a Workbook object by excel file path
361+
workbook = self.Workbook()
362+
363+
# Accessing the first worksheet in the Excel file
364+
worksheet = workbook.getWorksheets().get(0)
365+
366+
# Put some data into header rows (A1:A4)
367+
i = 0
368+
while i < 5:
369+
worksheet.getCells().get(i, 0).setValue("Header Row #i")
370+
371+
372+
373+
374+
375+
# Put some detail data (A5:A999)
376+
i = 5
377+
while i < 1000:
378+
worksheet.getCells().get(i, 0).setValue("Detail Row #i")
379+
380+
381+
# Create another Workbook.
382+
workbook1 = Workbook()
383+
384+
# Get the first worksheet in the book.
385+
worksheet1 = workbook1.getWorksheets().get(0)
386+
387+
# Copy the first column from the first worksheet of the first workbook into
388+
# the first worksheet of the second workbook.
389+
worksheet1.getCells().copyColumn(worksheet.getCells(),0,2)
390+
391+
# Autofit the column.
392+
worksheet1.autoFitColumn(2)
393+
394+
# Saving the modified Excel file in default (that is Excel 2003) format
395+
workbook.save(self.dataDir + "Copy Columns.xls")
396+
397+
print "Copy Columns Successfully."
Binary file not shown.

0 commit comments

Comments
 (0)