Bạn có thể sử dụng Python trong Excel không

Tất cả các bạn chắc hẳn đã từng làm việc với Excel vào một lúc nào đó trong đời và chắc hẳn đã cảm thấy cần phải tự động hóa một số tác vụ lặp đi lặp lại hoặc tẻ nhạt. Đừng lo lắng trong hướng dẫn này, chúng ta sẽ tìm hiểu về cách làm việc với Excel bằng Python hoặc tự động hóa Excel bằng Python. Chúng tôi sẽ trình bày vấn đề này với sự trợ giúp của mô-đun Openpyxl

 

Bắt đầu

Openpyxl là một thư viện Python cung cấp nhiều phương thức khác nhau để tương tác với Tệp Excel bằng Python. Nó cho phép các hoạt động như đọc, viết, phép toán số học, vẽ đồ thị, v.v.

Mô-đun này không được tích hợp sẵn với Python. Để cài đặt loại này, lệnh dưới đây trong thiết bị đầu cuối

pip install openpyxl

Đọc từ bảng tính

Để đọc tệp Excel, bạn phải mở bảng tính bằng phương thức load_workbook(). Sau đó, bạn có thể sử dụng kích hoạt để chọn trang tính đầu tiên có sẵn và thuộc tính ô để chọn ô bằng cách chuyển tham số hàng và cột. Thuộc tính giá trị in giá trị của ô cụ thể. Xem ví dụ bên dưới để hiểu rõ hơn.  

Ghi chú. Số nguyên của hàng hoặc cột đầu tiên là 1, không phải 0

Tập dữ liệu được sử dụng. Nó có thể được tải về từ đây

Thí dụ

Python3




# Python program to read an excel file 

 

# import openpyxl module 

import openpyxl 

 

Name0

Name1Name2 Name3

 

Name5

Name6

Name7Name2 Name9

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 1

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 2

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 3Name2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 5

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 7

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 8

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 9

 

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech1

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech2

 

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech4

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech5

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech6Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech8Name2 # Python program to read an excel file 0# Python program to read an excel file 1Name2 # Python program to read an excel file 0_______427_______4

 

# Python program to read an excel file 6

# Python program to read an excel file 7

# Python program to read an excel file 8_______427_______9

đầu ra

Name

Đọc từ nhiều ô

Có thể có hai cách đọc từ nhiều ô.  

Phương pháp 1. Chúng ta có thể lấy tổng số hàng và cột bằng cách sử dụng max_row và max_column tương ứng. Chúng ta có thể sử dụng các giá trị này bên trong vòng lặp for để lấy giá trị của hàng hoặc cột mong muốn hoặc bất kỳ ô nào tùy thuộc vào tình huống. Hãy xem cách lấy giá trị của cột đầu tiên và hàng đầu tiên

Thí dụ

Python3




# Python program to read an excel file 

 

# import openpyxl module 

import openpyxl 

 

Name0

Name1Name2 Name3

 

Name5

Name6

Name7Name2 Name9

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 1

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 2

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 3Name2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 5

 

import3

import4

import5____1_______2 import7

import8____1_______2 openpyxl 0

 

# Python program to read an excel file 8_______431_______3openpyxl 4openpyxl 5

# Python program to read an excel file 8_______431_______3openpyxl 8openpyxl 9

 

 1

 2

 3

# Python program to read an excel file 8_______431_______3 6 7

 8  9Name00 Name01openpyxl 3# Python program to read an excel file 0Name04Name05 # Python program to read an excel file 0Name07

Name08Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech6Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech8_______1_______2 Name13Name2 # Python program to read an excel file 0# Python program to read an excel file 4

Name08# Python program to read an excel file 8# Python program to read an excel file 9

Name20

 1

 2

Name23

# Python program to read an excel file 8_______431_______3Name26 7

 8  9Name00 Name01_______431_______3# Python program to read an excel file 0# Python program to read an excel file 1Name05 # Python program to read an excel file 0Name07

Name08Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech6Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech8_______1_______2 Name43# Python program to read an excel file 1Name2 Name46

Name08# Python program to read an excel file 8Name49Name2 Name51 7

đầu ra

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4

Phương pháp 2. Chúng ta cũng có thể đọc từ nhiều ô bằng cách sử dụng tên ô. Đây có thể được coi là danh sách cắt của Python

Python3




# Python program to read an excel file 

 

# import openpyxl module 

import openpyxl 

 

Name0

Name1Name2 Name3

 

Name5

Name6

Name7Name2 Name9

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 1

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 2

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 3Name2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 5

 

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech4

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech5

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech6Name2 Name80Name81Name82Name83Name84

 

# Python program to read an excel file 6

# Python program to read an excel file 7

 8 Name89Name00 Name91

Name08# Python program to read an excel file 8Name94

đầu ra

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech

Tham khảo bài viết dưới đây để biết thông tin chi tiết về cách đọc file excel bằng openpyxl

  • Đọc tệp excel bằng mô-đun Python openpyxl

Viết vào bảng tính

Trước tiên, hãy tạo một bảng tính mới, sau đó chúng tôi sẽ ghi một số dữ liệu vào tệp mới tạo. Một bảng tính trống có thể được tạo bằng phương thức Workbook(). Hãy xem ví dụ dưới đây

Thí dụ

Python3




Name95 Name96_______430_______ Name98

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 00

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 01

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 02____1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 04

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 06

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 07

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 08

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 09

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 10Name2Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 12 7

đầu ra

Sau khi tạo một tệp trống, hãy xem cách thêm một số dữ liệu vào tệp đó bằng Python. Để thêm dữ liệu, trước tiên chúng ta cần chọn trang tính đang hoạt động, sau đó sử dụng phương thức cell(), chúng ta có thể chọn bất kỳ ô cụ thể nào bằng cách chuyển số hàng và cột làm tham số của nó. Chúng ta cũng có thể viết bằng cách sử dụng tên ô. Xem ví dụ dưới đây để hiểu rõ hơn

Thí dụ

Python3




# import openpyxl module 

import openpyxl 

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 18

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 01

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 22

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 24

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 2

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 28

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 30

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 8

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 9

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 34

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 35

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 36

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 37Name2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39_______1_______2 # Python program to read an excel file 0# Python program to read an excel file 1Name2 # Python program to read an excel file 0# Python program to read an excel file 4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 47

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 48_______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 50

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 52_______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39_______1_______2 # Python program to read an excel file 0 # Python program to read an excel file 1Name2 Name43# Python program to read an excel file 4

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 61____1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 63

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 65

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 66

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 67

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 68Name2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 71Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 72

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 73Name2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 75

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 77

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 78Name2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 81Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 72

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 83____1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 85

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 06

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 07

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 08

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 09

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____10_______12# Python program to read an excel file 4

đầu ra

Tham khảo bài viết dưới đây để biết thông tin chi tiết về cách viết excel

  • Ghi vào tệp excel bằng mô-đun openpyxl

Thêm vào bảng tính

Trong ví dụ trên, bạn sẽ thấy rằng mỗi khi bạn cố gắng ghi vào bảng tính, dữ liệu hiện có sẽ bị ghi đè và tệp được lưu dưới dạng tệp mới. Điều này xảy ra vì phương thức Workbook() luôn tạo đối tượng tệp sổ làm việc mới. Để ghi vào sổ làm việc hiện có, bạn phải mở tệp bằng phương thức load_workbook(). Chúng tôi sẽ sử dụng sổ làm việc được tạo ở trên

Thí dụ

Python3




# import openpyxl module 

import openpyxl 

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech00Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 12_______427_______4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 28

 

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech08Name2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech11Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 72

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech13______1_______2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech15

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____10_______12 7

đầu ra

Chúng ta cũng có thể sử dụng phương thức append() để nối nhiều dữ liệu vào cuối trang tính

Thí dụ

Python3




# import openpyxl module 

import openpyxl 

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech00Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 12_______427_______4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 28

 

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech34______1_______2 openpyxl 3

Name08openpyxl 3# Python program to read an excel file 0Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40Name43Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech43Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech44

Name08openpyxl 3Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech47Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech49Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech51_______428_______7

 7

 

 8 import5Name00 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech58

Name08Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech60

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____18_______63 7

đầu ra

Phép toán số học trên bảng tính

Các phép toán số học có thể được thực hiện bằng cách nhập công thức vào một ô cụ thể của bảng tính. Ví dụ muốn tính tổng thì sử dụng công thức =Sum() của file excel

Thí dụ

Python3




# import openpyxl module 

import openpyxl 

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 18

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 01

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 22

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 24

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech76

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 28

 

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech81

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70_______1_______81Name84Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech86

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70_______10_______71Name84Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech91

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70_______18_______11Name84Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech96

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70_______18_______98Name84Name2 # Python program to read an excel file 01

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70_______427_______03Name84Name2 # Python program to read an excel file 06

 

# Python program to read an excel file 08

# Python program to read an excel file 09

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 70_______427_______11Name84Name2 # Python program to read an excel file 14

 

# Python program to read an excel file 16

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____427_______18# Python program to read an excel file 4

đầu ra

Tham khảo bài viết dưới đây để biết thông tin chi tiết về các phép toán Số học trên Bảng tính

  • Các phép toán số học trong tệp excel bằng openpyxl

Điều chỉnh hàng và cột

Các đối tượng trang tính có các thuộc tính row_dimensions và column_dimensions kiểm soát chiều cao của hàng và chiều rộng của cột. Row_dimensions và column_dimensions của trang tính là các giá trị giống như từ điển; . Trong row_dimensions, người ta có thể truy cập một trong các đối tượng bằng cách sử dụng số của hàng (trong trường hợp này là 1 hoặc 2). Trong column_dimensions, người ta có thể truy cập một trong các đối tượng bằng chữ cái của cột (trong trường hợp này là A hoặc B)

Thí dụ

Python3




# import openpyxl module 

import openpyxl 

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 18

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 01

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 22

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 24

Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech76

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 28

 

# Python program to read an excel file 36

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 # Python program to read an excel file 0# Python program to read an excel file 1Name2 # Python program to read an excel file 0# Python program to read an excel file 43Name2 # Python program to read an excel file 45

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name43# Python program to read an excel file 1Name2 Name43# Python program to read an excel file 43Name2 # Python program to read an excel file 55

 

# Python program to read an excel file 57

# Python program to read an excel file 58# Python program to read an excel file 0# Python program to read an excel file 60Name2 # Python program to read an excel file 62

 

# Python program to read an excel file 64

# Python program to read an excel file 65_______427_______66____427_______67_______1_______2 # Python program to read an excel file 69

 

# Python program to read an excel file 16

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____18_______63# Python program to read an excel file 4

đầu ra

Hợp nhất các ô

Một vùng ô hình chữ nhật có thể được hợp nhất thành một ô duy nhất bằng phương thức trang tính merge_cells(). Đối số cho merge_cells() là một chuỗi gồm các ô trên cùng bên trái và dưới cùng bên phải của vùng hình chữ nhật sẽ được hợp nhất

Thí dụ

Python3




import openpyxl 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 22

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 28

 

# Python program to read an excel file 84

# Python program to read an excel file 85

# Python program to read an excel file 86

# Python program to read an excel file 87_______427_______88# Python program to read an excel file 4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name43# Python program to read an excel file 1Name2 # Python program to read an excel file 0# Python program to read an excel file 43Name2 # Python program to read an excel file 99

 

 01

# Python program to read an excel file 87_______428_______03# Python program to read an excel file 4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech51_______427_______1Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech51# Python program to read an excel file 43Name2  14

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____18_______63 7

đầu ra

Hợp nhất các ô

Để hủy hợp nhất các ô, hãy gọi phương thức trang tính unmerge_cells()

Thí dụ

Python3




import openpyxl 

 

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech00Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech63# Python program to read an excel file 4

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 28

 

 32

 33______427_______88# Python program to read an excel file 4

 

 33______428_______03# Python program to read an excel file 4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____18_______63 7

đầu ra

Đặt kiểu phông chữ

Để tùy chỉnh kiểu phông chữ trong các ô, quan trọng, hãy nhập hàm Font() từ openpyxl. mô-đun phong cách

Thí dụ

Python3




import openpyxl 

 

 47

Name95  49import  51

 

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 22

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 28

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 # Python program to read an excel file 0# Python program to read an excel file 1Name2 # Python program to read an excel file 0# Python program to read an excel file 43Name2  69

 

 71

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 # Python program to read an excel file 0# Python program to read an excel file 1Name2 # Python program to read an excel file 0 78Name2  80Name2  82 # Python program to read an excel file 4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name43# Python program to read an excel file 1Name2 Name43# Python program to read an excel file 43Name2  69

 

 95

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name43# Python program to read an excel file 1Name2 Name43 78Name2  80Name2  82# import openpyxl module 07Name2 # import openpyxl module 09# Python program to read an excel file 4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech43# Python program to read an excel file 1Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech43# Python program to read an excel file 43Name2  69

 

# import openpyxl module 22

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech43# Python program to read an excel file 1Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech43 78Name2  80Name2  82# import openpyxl module 34Name2 # import openpyxl module 09# Python program to read an excel file 4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech47_______427_______1Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech47# Python program to read an excel file 43Name2  69

 

# import openpyxl module 49

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 39Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech47_______427_______1Name2 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech47 78Name2  80Name2  82# import openpyxl module 61Name2 # import openpyxl module 63# Python program to read an excel file 4

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____18_______63# Python program to read an excel file 4

đầu ra

Tham khảo bài viết dưới đây để biết thông tin chi tiết về điều chỉnh hàng và cột

  • Điều chỉnh các hàng và cột của tệp excel bằng mô-đun openpyxl

vẽ biểu đồ

Biểu đồ bao gồm ít nhất một chuỗi một hoặc nhiều điểm dữ liệu. Bản thân chuỗi bao gồm các tham chiếu đến phạm vi ô. Để vẽ biểu đồ trên trang tính excel, trước tiên, hãy tạo các đối tượng biểu đồ của lớp biểu đồ cụ thể ( i. e Biểu đồ thanh, Biểu đồ đường, v.v. ). Sau khi tạo các đối tượng biểu đồ, hãy chèn dữ liệu vào đó và cuối cùng, thêm đối tượng biểu đồ đó vào đối tượng trang tính

ví dụ 1

Python3




# import openpyxl module 69

import Name96

 

# import openpyxl module 73

Name95 # import openpyxl module 75import # import openpyxl module 77

 

# import openpyxl module 79

# import openpyxl module 80

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20______1_______2 # import openpyxl module 83

 

# import openpyxl module 85

# import openpyxl module 86

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 # import openpyxl module 89

 

# import openpyxl module 91

 8  9Name00 Name01openpyxl 3# import openpyxl module 97# import openpyxl module 98

Name08import00

 

import02

import03Name2 import05Name2# Python program to read an excel file 0import08Name2# Python program to read an excel file 0Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40

import12import13Name2# Python program to read an excel file 0import16Name2# import openpyxl module 97_______428_______7

 

import21

import22Name2 import24

 

import26

import27

 

import29

import30Name2 import32

 

import34

import35______1_______2 import37

 

import39

import40Name2 import42

 

import44

import45

import46

import47____430_______48 7

 

import51

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____10_______12 7

đầu ra

ví dụ 2

Python3




# import openpyxl module 69

import Name96

 

import59

Name95 # import openpyxl module 75import import63

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20______1_______2 # import openpyxl module 83

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 # import openpyxl module 89

 

# import openpyxl module 91

 8  9Name00 Name01openpyxl 3# import openpyxl module 97# import openpyxl module 98

Name08import00

 

import03Name2 import05Name2# Python program to read an excel file 0import08Name2# Python program to read an excel file 0Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40

import12import13Name2# Python program to read an excel file 0import16Name2# import openpyxl module 97_______428_______7

 

openpyxl 01

import22____1_______2 openpyxl 04

 

import27

 

import29

import30Name2 openpyxl 11

 

import34

import35______1_______2 openpyxl 16

 

import39

import40Name2 openpyxl 21

 

import44

import45

import46

import47____430_______48 7

 

import51

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 91____10_______12 7

đầu ra

Tham khảo các bài viết dưới đây để biết thông tin chi tiết về vẽ đồ thị trong excel bằng Python

  • Vẽ biểu đồ trong bảng excel bằng mô-đun openpyxl. Hiệp 1
  • Vẽ biểu đồ trong bảng excel bằng mô-đun openpyxl. Đặt  2
  • Vẽ biểu đồ trong bảng excel bằng mô-đun openpyxl. bộ 3

Thêm hình ảnh

Với mục đích nhập hình ảnh bên trong trang tính của chúng tôi, chúng tôi sẽ sử dụng openpyxl. đang vẽ. hình ảnh. Hình ảnh. Phương pháp này là một trình bao bọc trên PIL. Phương pháp hình ảnh được tìm thấy trong thư viện PIL (gối). Do đó, cần phải cài đặt thư viện PIL (gối) để sử dụng phương pháp này

Hình ảnh được sử dụng

Thí dụ

Python3




import openpyxl 

Name95 openpyxl 37import openpyxl 39

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 20_______1_______2 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 22

 

Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 4 26______1_______2 # import openpyxl module 89

 

openpyxl 49

openpyxl 50

openpyxl 51_______429_______97Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40openpyxl 54Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40openpyxl 56Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech47Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech40openpyxl 60openpyxl 61

Bạn có thể sử dụng Python trong Excel thay vì VBA không?

CÔNG CỤ MACRO VÀ EXCEL . g. thông qua một nút trên trang tính. Điều này cũng hoạt động tốt cho các ứng dụng web tạo mẫu (Windows & Mac). Write Excel tools with Python instead of VBA and call your code directly from within Excel, e.g. via a button on the sheet. This also works great for prototyping web apps (Windows & Mac).

Python có tốt với Excel không?

Excel có thể là trở ngại lớn khi bạn đang cố tự động hóa một quy trình hoặc chạy nhiều lần cùng một phân tích. Sử dụng Python có thể giúp việc này nhanh hơn nhiều .

Bạn có thể sử dụng Python để tự động hóa Excel không?

Nhìn chung, Tự động hóa Python Excel là một quy trình sáng tạo mà bạn có thể sử dụng để tạo báo cáo trực quan trên Python giống như cách bạn làm trên Excel một cách liền mạch. Organizations can leverage Python Excel Automation to leverage their business tasks according to their needs.

Bạn có thể sử dụng các hàm Python trong Excel không?

Các hàm trang tính Excel hoặc UDF (Hàm do người dùng xác định) là cách trực quan nhất để gọi các hàm Python trong Excel . Chắc hẳn bạn đã quen với việc gọi các hàm trang tính trong Excel rồi. Nếu bạn đã sử dụng SUM để tính tổng một cột số thì bạn đã sử dụng một hàm trang tính.

Chủ đề