Tạo bảng tính Excel trong Python - Quản lý hàng/cột
Chèn một hàng
Chèn một hàng tại bất kỳ vị trí nào bằng cách gọi & nbsp; insertrows & nbsp; phương thức của & nbsp; cell & nbsp; thu thập. Phương thức & nbsp; Insertrows & nbsp; lấy chỉ mục của hàng trong đó hàng mới sẽ được chèn làm đối số đầu tiên và số lượng hàng được chèn làm đối số thứ hai. Sau đây là các bước:
- Tải Sổ làm việc XLS hoặc XLSX
- Truy cập bảng tính
- Chèn hàng
- Lưu dưới dạng XLS hoặc XLSX Workbook
Mã Python
def insert_row[self]:
\# Instantiating a Workbook object by excel file path
workbook = self.Workbook[self.dataDir + "Book1.xls"]
\# Accessing the first worksheet in the Excel file
worksheet = workbook.getWorksheets[].get[0]
\# Inserting a row into the worksheet at 3rd position
worksheet.getCells[].insertRows[2,1]
\# Saving the modified Excel file in default [that is Excel 2003] format
workbook.save[self.dataDir + "Insert Row.xls"]
print "Insert Row Successfully."
Chèn nhiều hàng
Để chèn nhiều hàng vào bảng tính, hãy gọi & nbsp; Insertrows & nbsp; Phương thức của & nbsp; cell & nbsp; thu thập. Phương thức & nbsp; insertrows & nbsp; lấy hai tham số:
- Chỉ mục hàng, chỉ mục của hàng từ nơi các hàng mới sẽ được chèn.
- Số lượng hàng, tổng số hàng cần được chèn.
Mã Python
def insert_multiple_rows[self]:
\# Instantiating a Workbook object by excel file path
workbook = self.Workbook[self.dataDir + 'Book1.xls']
\# Accessing the first worksheet in the Excel file
worksheet = workbook.getWorksheets[].get[0]
\# Inserting a row into the worksheet at 3rd position
worksheet.getCells[].insertRows[2,10]
\# Saving the modified Excel file in default [that is Excel 2003] format
workbook.save[self.dataDir + "Insert Multiple Rows.xls"]
print "Insert Multiple Rows Successfully."
Chèn nhiều hàng
Để chèn nhiều hàng vào bảng tính, hãy gọi & nbsp; Insertrows & nbsp; Phương thức của & nbsp; cell & nbsp; thu thập. Phương thức & nbsp; insertrows & nbsp; lấy hai tham số:
- Chỉ mục hàng, chỉ mục của hàng từ nơi các hàng mới sẽ được chèn.
- Số lượng hàng, tổng số hàng cần được chèn.
Mã Python
def delete_row[self]:
\# Instantiating a Workbook object by excel file path
workbook = self.Workbook[self.dataDir + 'Book1.xls']
\# Accessing the first worksheet in the Excel file
worksheet = workbook.getWorksheets[].get[0]
\# Deleting 3rd row from the worksheet
worksheet.getCells[].deleteRows[2,1,True]
\# Saving the modified Excel file in default [that is Excel 2003] format
workbook.save[self.dataDir + "Delete Row.xls"]
print "Delete Row Successfully."
Chèn nhiều hàng
Để chèn nhiều hàng vào bảng tính, hãy gọi & nbsp; Insertrows & nbsp; Phương thức của & nbsp; cell & nbsp; thu thập. Phương thức & nbsp; insertrows & nbsp; lấy hai tham số:
- Chỉ mục hàng, chỉ mục của hàng từ nơi các hàng mới sẽ được chèn.
- Số lượng hàng, tổng số hàng cần được chèn.
Mã Python
def delete_multiple_rows[self]:
\# Instantiating a Workbook object by excel file path
workbook = self.Workbook[self.dataDir + 'Book1.xls']
\# Accessing the first worksheet in the Excel file
worksheet = workbook.getWorksheets[].get[0]
\# Deleting 10 rows from the worksheet starting from 3rd row
worksheet.getCells[].deleteRows[2,10,True]
\# Saving the modified Excel file in default [that is Excel 2003] format
workbook.save[self.dataDir + "Delete Multiple Rows.xls"]
print "Delete Multiple Rows Successfully."
Chèn nhiều hàng
Để chèn nhiều hàng vào bảng tính, hãy gọi & nbsp; Insertrows & nbsp; Phương thức của & nbsp; cell & nbsp; thu thập. Phương thức & nbsp; insertrows & nbsp; lấy hai tham số:
- Chỉ mục hàng, chỉ mục của hàng từ nơi các hàng mới sẽ được chèn.
- Số lượng hàng, tổng số hàng cần được chèn.
Mã Python
def insert_column[self]:
\# Instantiating a Workbook object by excel file path
workbook = self.Workbook[self.dataDir + 'Book1.xls']
\# Accessing the first worksheet in the Excel file
worksheet = workbook.getWorksheets[].get[0]
\# Inserting a column into the worksheet at 2nd position
worksheet.getCells[].insertColumns[1,1]
\# Saving the modified Excel file in default [that is Excel 2003] format
workbook.save[self.dataDir + "Insert Column.xls"]
print "Insert Column Successfully."
Chèn nhiều hàng
Để chèn nhiều hàng vào bảng tính, hãy gọi & nbsp; Insertrows & nbsp; Phương thức của & nbsp; cell & nbsp; thu thập. Phương thức & nbsp; insertrows & nbsp; lấy hai tham số:
- Chỉ mục hàng, chỉ mục của hàng từ nơi các hàng mới sẽ được chèn.
- Số lượng hàng, tổng số hàng cần được chèn.
- Xóa một hàng
Mã Python
def delete_column[self]:
\# Instantiating a Workbook object by excel file path
workbook = self.Workbook[self.dataDir + 'Book1.xls']
\# Accessing the first worksheet in the Excel file
worksheet = workbook.getWorksheets[].get[0]
\# Deleting a column from the worksheet at 2nd position
worksheet.getCells[].deleteColumns[1,1,True]
\# Saving the modified Excel file in default [that is Excel 2003] format
workbook.save[self.dataDir + "Delete Column.xls"]
print "Delete Column Successfully."
Chèn nhiều hàng
Để chèn nhiều hàng vào bảng tính, hãy gọi & nbsp; Insertrows & nbsp; Phương thức của & nbsp; cell & nbsp; thu thập. Phương thức & nbsp; insertrows & nbsp; lấy hai tham số:Managing Rows/Columns [Aspose.Cells] from any of the below mentioned social coding sites:
- Chỉ mục hàng, chỉ mục của hàng từ nơi các hàng mới sẽ được chèn.