Hướng dẫn does excel and python work together? - excel và python có hoạt động cùng nhau không?
Tất cả các bạn phải làm việc với Excel tại một số thời điểm trong cuộc sống của bạn và phải cảm thấy cần phải tự động hóa một số nhiệm 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 tôi sẽ tìm hiểu về cách làm việc với Excel bằng cách sử dụng Python hoặc tự động hóa Excel bằng Python. Chúng tôi sẽ đề cập đến điều này với sự trợ giúp của mô -đun OpenPyXL. Show
Bắt đầuOpenPyXL là một thư viện Python cung cấp các phương pháp khác nhau để tương tác với các tệp Excel bằng Python. Nó cho phép các hoạt động như đọc, viết, hoạt động số học, vẽ đồ thị, v.v.is a Python library that provides various methods to interact with Excel Files using Python. It allows operations like reading, writing, arithmetic operations, plotting graphs, etc. Mô-đun này không được xây dựng 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 một 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 hoạt động để chọn bảng đầ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ụ dưới đây để hiểu rõ hơn. & NBSP;load_workbook() method. After that, you can use the active to select the first sheet available and the cell attribute to select the cell by passing the row and column parameter. The value attribute prints the value of the particular cell. See the below example to get a better understanding. Lưu ý: Hàng đầu tiên hoặc số nguyên cột là 1, không phải 0.The first row or column integer is 1, not 0. Bộ dữ liệu được sử dụng: Nó có thể được tải xuống từ đây. It can be downloaded from here. Example: Python3
Name1 Name2 = Name4 Name5 = Name7 = 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 44 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 45 Output: Name Đọc từ nhiều ôCó thể có hai cách đọc từ nhiều ô. & NBSP; Phương pháp 1: Chúng ta có thể nhận được số lượng của 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 để có được 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 cùng xem cách nhận được giá trị của cột đầu tiên và hàng đầu tiên.We can get the count of the total rows and columns using the max_row and max_column respectively. We can use these values inside the for loop to get the value of the desired row or column or any cell depending upon the situation. Let’s see how to get the value of the first column and first row. Example: Python3
Name1 Name2 = Name4 Name5 = Name7 = Name9______ Đọc từ nhiều ô Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 44 import 4import 5import 6Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 44 import 4import 9openpyxl 0Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 44 import 4openpyxl 3openpyxl 4Có thể có hai cách đọc từ nhiều ô. & NBSP; Phương pháp 1: Chúng ta có thể nhận được số lượng của 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 để có được 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 cùng xem cách nhận được giá trị của cột đầu tiên và hàng đầu tiên.
Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 44 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 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 44 import 4= 9openpyxl 4Python3 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech7 = Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech9
Output: 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
Python3
Name1 Name2 = Name4 Name5 = Name7 = Name9______ Đọc từ nhiều ô
Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 44 Name30 Output: Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech Có thể có hai cách đọc từ nhiều ô. & NBSP;
Python3Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech7 = Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech9Workbook() method. Let’s see the below example. Example: Python3
Name38 = Name40 openpyxl 4Output:
Name5 = Name7 = = 0__Example: Python3
Name1 Name2 = Name4 Name5 = Name7 = Name9______ Đọc từ nhiều ô Có thể có hai cách đọc từ nhiều ô. & NBSP; Phương pháp 1: Chúng ta có thể nhận được số lượng của 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 để có được 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 cùng xem cách nhận được giá trị của cột đầu tiên và hàng đầu tiên. Python3 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech7 = Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech9
Name90 Name40 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Output:
Nối vào bảng tínhTrong 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ó bị ghi đè và tệp được lưu dưới dạng tệp mới. Điều này xảy ra bởi vì phương thức Workbook () luôn tạo một đố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 ra ở trên.Workbook() method always creates a new workbook file object. To write to an existing workbook you must open the file with the load_workbook() method. We will use the above-created workbook. Example: Python3
Name44 = Name97 Name40 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name47 = Name49 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 403 = Name76 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 406 Name78 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 408 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 410 Name90 Name40 openpyxl 4Output: Chúng tôi cũng có thể sử dụng phương thức append () để nối nhiều dữ liệu ở cuối trang.append() method to append multiple data at the end of the sheet. Example: Python3
Name44 = Name97 Name40 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name47 = Name49 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 403 = Name76 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 406 Name78
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 430 wb_obj 6Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 430 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 433 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 434
Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 437 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 430 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 439 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 430 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 441 openpyxl 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 408 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 410
Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 449 Name90 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 451 openpyxl 4Output: Chúng tôi cũng có thể sử dụng phương thức append () để nối nhiều dữ liệu ở cuối trang.Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 424 = import 4 =Sum() formula of the excel
file is used.Example: Python3
Name44 = Name97 Name40 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name47 = Name49 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 403 = Name76 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 406 Name78 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 408 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 410 Chúng tôi cũng có thể sử dụng phương thức append () để nối nhiều dữ liệu ở cuối trang. Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 424 = import 4
Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech7 openpyxl 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 447 Hoạt động số học trên bảng tính Các hoạt động số học có thể được thực hiện bằng cách gõ công thức trong một ô cụ thể của bảng tính. Ví dụ: nếu chúng ta muốn tìm tổng công thức SUM sau đó = sum () của tệp excel được sử dụng. Output: Name44 = Name46
Name76Name777____263= 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 70Name76 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 406 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 463 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 475 Example: Python3
Name44 = Name97 Name40 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name47 = Name49 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 403 = Name76 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 406 Name78 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 408 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 410 Chúng tôi cũng có thể sử dụng phương thức append () để nối nhiều dữ liệu ở cuối trang. Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 424 = import 4Name90 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 451 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Output: openpyxl 5 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech7openpyxl 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 47Hoạt động số học trên bảng tính Example: Python3
Name44 = Name97 Name40 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name47 = Name49 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech41 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech42 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 403 = Name76 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 406 Name78 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech41 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech54 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 408 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 410 Name90 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 451 openpyxl 4
Output: Chúng tôi cũng có thể sử dụng phương thức append () để nối nhiều dữ liệu ở cuối trang.Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 424 = import 4Example: Python3
Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech7 openpyxl 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 447 Name47 = Name49 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech78 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech42 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech78 Name Course Ankit B.Tech Rahul M.Tech Priya MBA Nikhil B.Tech Nisha B.Tech54 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name90 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 451 openpyxl 4Output: Hoạt động số học trên bảng tínhCác hoạt động số học có thể được thực hiện bằng cách gõ công thức trong một ô cụ thể của bảng tính. Ví dụ: nếu chúng ta muốn tìm tổng công thức SUM sau đó = sum () của tệp excel được sử dụng.Font() function from the openpyxl.styles module. Example: Python3
Name44 = Name46 Name44 = Name97 Name40 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name47 = Name49 Name47 = Name49 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 403 = Name76 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 406 Name78 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 408 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 410 Chúng tôi cũng có thể sử dụng phương thức append () để nối nhiều dữ liệu ở cuối trang. Name52 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 433______ Name52 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 433______ Name52 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 437______ Name52 = Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 437______ Name90 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 451 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Output: Tham khảo bài viết dưới đây để có thông tin chi tiết về việc điều chỉnh các hàng và cột.
Biểu đồ biểu đồBiểu đồ bao gồm ít nhất một loạt một hoặc nhiều điểm dữ liệu. Bản thân loạt bao gồm các tài liệu tham khảo về phạm vi ô. Để vẽ các biểu đồ trên một tờ Excel, trước tiên, hãy tạo các đối tượng biểu đồ của lớp biểu đồ cụ thể (tức là Barchart, Linechart, v.v.). Sau khi tạo các đối tượng biểu đồ, 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
Name31 import 98import openpyxl 00Name44 = openpyxl 03Name47 = openpyxl 06
Name9 openpyxl 21= Name9 openpyxl 24Các
Name90 Name40 openpyxl 4Output: Ví dụ 2: Python3
Name31 import 98import openpyxl 00Name44 = openpyxl 03Name47 = openpyxl 06
Name9 openpyxl 21= Name9 openpyxl 24
Name9 openpyxl 29= openpyxl 12openpyxl 4Các
Name90 Name40 openpyxl 4
Output:
openpyxl 37= openpyxl 96
Example: Python3Tham khảo các bài viết dưới đây để có được thông tin chi tiết về âm mưu excel bằng Python. Biểu đồ biểu đồ trong trang tính Excel bằng mô -đun OpenPyXL | Đặt & nbsp; 1 Biểu đồ biểu đồ trong trang tính Excel bằng mô -đun OpenPyXL | Đặt & NBSP; 2 Name47 = openpyxl 06
Name9 openpyxl 21= Name9 openpyxl 24
Name77 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 43 Name90 Total Rows: 6 Total Columns: 4 Value of first column Name Ankit Rahul Priya Nikhil Nisha Value of first row Ankit B.Tech CSE 451 openpyxl 4Output: Các
openpyxl 37= openpyxl 39
|