Hướng dẫn xlrd read specific sheet - xlrd đọc trang cụ thể
Đến bây giờ tôi có thể đọc tất cả các trang tính của Excel. Show
Đã hỏi ngày 7 tháng 2 năm 2018 lúc 12:22Feb 7, 2018 at 12:22
1 Nếu bạn mở trình chỉnh sửa của mình từ dòng máy tính để bàn hoặc dòng lệnh, bạn sẽ phải chỉ định đường dẫn tệp trong khi cố gắng đọc tệp:
Ngoài ra, nếu bạn mở trình chỉnh sửa của mình trong thư mục của tệp, thì bạn có thể đọc trực tiếp bằng thư viện Panda
Đã trả lời ngày 23 tháng 7 năm 2019 lúc 9:59Jul 23, 2019 at 9:59
Có thể sẽ hữu ích (gói dữ liệu cho dữ liệu):
EDIT: Vì rất nhiều thời gian đã trôi qua và Pandas trưởng thành, các cuộc tranh luận đã thay đổi. Vì vậy, đối với gấu trúc> 1.0.0
Đã trả lời ngày 7 tháng 2 năm 2018 lúc 12:27Feb 7, 2018 at 12:27
2 Bạn có thể sử dụng 0 để đọc các tờ cụ thể bằng tên của họ từ tệp XLS.
1 là đường dẫn đến tệp XLS của bạn. Chỉ định số trang bạn cần đọc trong 2.Ngoài ra, bạn có thể sử dụng 3 và truyền đối số để trả về một tờ cụ thể. Từ tài liệu:
Ví dụ:
Đã trả lời ngày 7 tháng 2 năm 2018 lúc 12:30Feb 7, 2018 at 12:30
AustinaustinAustin 25.4K4 Huy hiệu vàng23 Huy hiệu bạc48 Huy hiệu đồng4 gold badges23 silver badges48 bronze badges 2 Bạn có thể sử dụng một trong hai cuốn sách.sheet_by_name () hoặc book.get_sheet ()book.sheet_by_name() or book.get_sheet() Ví dụ sử dụng get_sheet () using get_sheet()
Ví dụ sử dụng sheet_by_name () using sheet_by_name()
MoreInfo khi nhận được tờ bằng bảng_by_name on getting sheet by sheet_by_name Đã trả lời ngày 7 tháng 2 năm 2018 lúc 12:38Feb 7, 2018 at 12:38
RakeshrakeshRakesh 79,9K17 Huy hiệu vàng72 Huy hiệu bạc109 Huy hiệu đồng17 gold badges72 silver badges109 bronze badges The .xlsx is the extension of the excel document that can store a large amount of data in tabular form, and many types of arithmetic and logical calculation can be done easily in an excel spreadsheet. Đôi khi cần phải đọc dữ liệu từ tài liệu Excel bằng tập lệnh Python cho mục đích lập trình. Nhiều mô -đun tồn tại trong Python để đọc tài liệu Excel. Một số mô -đun hữu ích là XLRD, OpenPyXL và Pandas. Các cách để sử dụng các mô -đun này để đọc tệp Excel trong Python đã được hiển thị trong hướng dẫn này..xlsx is the extension of the excel document that can store a large amount of data in tabular form, and many types of arithmetic and logical calculation can be done easily in an excel spreadsheet. Sometimes it is required to read the data from the excel document using Python script for programming purposes. Many modules exist in Python to read the excel document. Some of the useful modules are xlrd, openpyxl, and pandas. The ways to use these modules to read the excel file in Python have been shown in this tutorial. .xlsx is the extension of the excel document that can store a large amount of data in tabular form, and many types of arithmetic and logical calculation can be done easily in an excel spreadsheet. Sometimes it is required to read the data from the excel document using Python script for programming purposes. Many modules exist in Python to read the excel document. Some of the useful modules are xlrd, openpyxl,
and pandas. The ways to use these modules to read the excel file in Python have been shown in this tutorial. Nội dung chính ShowShow Pre-requisite:Bởi vì các phiên bản cũ hơn 1.2.0 sử dụng thư viện XLRD để đọc các tệp Excel.Thư viện XLRD chỉ hỗ trợ đọc.Tệp XLS.0 internally use the xlrd library to read the excel files. The xlrd library supports ONLY reading the . xls files.sales.xlsx file has been created with the following data. This file has used for reading by using different python modules in the next part of this tutorial. sales.xlsx
Ví dụ-1: Đọc tệp Excel bằng XLRDVí dụ-2: Đọc tệp Excel bằng OpenPyXLxlrd. $ pip installXLRD == 1.2.0pip installxlrd==1.2.0pip install xlrd==1.2.0 Sau khi hoàn thành quá trình cài đặt, hãy tạo tệp Python với tập lệnh sau để đọc tệp sales.xlsx bằng mô -đun XLRD. Hàm open_workbook () được sử dụng trong tập lệnh Mở tệp XLSX để đọc. Tệp Excel này chỉ chứa một tờ. Vì vậy, hàm workbook.sheet_by_index () đã được sử dụng trong tập lệnh với giá trị đối số 0. Tiếp theo, vòng lặp ‘cho vòng lặp đã được sử dụng để đọc các giá trị ô của bảng tính bằng cách sử dụng các giá trị hàng và cột. Hai hàm phạm vi () đã được sử dụng trong tập lệnh để xác định kích thước hàng và cột dựa trên dữ liệu trang tính. Hàm cell_value () đã được sử dụng để đọc giá trị ô cụ thể của trang tính trong mỗi lần lặp của vòng lặp. Mỗi trường trong đầu ra sẽ được phân tách bằng một không gian tab.sales.xlsx file using the xlrd module. open_workbook() function is used in the script open the xlsx file for reading. This excel file contains one sheet only. So, the workbook.sheet_by_index() function has been used in the script with the argument value 0. Next, the nested ‘for’ loop has used to read the cell values of the worksheet using the row and column values. Two range() functions have been used in the script to define the row and column size based on the sheet data. The cell_value() function has used to read the particular cell value of the sheet in each iteration of the loop. Each field in the output will be separated by one tab space.sales.xlsx file using the xlrd module. open_workbook() function is used in the script open the xlsx file for reading. This excel file contains one sheet only. So, the workbook.sheet_by_index() function has been used in the script with the argument value 0. Next, the nested ‘for’ loop has used to read the cell values of the worksheet using the row and column values. Two range() functions have been used in the script to define the row and column size based on the sheet data. The cell_value() function has used to read the particular cell value of the sheet in each iteration of the loop. Each field in the output will be separated by one tab space. # Nhập mô -đun XLRD Nhập XLRD import xlrd # Mở sổ làm việc trên sổ làm việc = xlrd.open_workbook ("sales.xlsx") workbook = xlrd.open_workbook("sales.xlsx") # Mở bảng tính bảng tính = workbook.sheet_by_index (0) worksheet = workbook.sheet_by_index(0) # Lặp lại các hàng và cột cho i trong phạm vi (0, 5): & nbsp; & nbsp; cho j trong phạm vi (0, 3): & nbsp; & nbsp; & nbsp; & nbsp; # In các giá trị ô bằng không gian tab & nbsp; & nbsp; & nbsp; & nbsp; in (bảng tính.cell_value (i, j), end = '\ t') & nbsp; & nbsp; in('')for i in range(0, 5): for j in range(0, 3): # Print the cell values with tab space print(worksheet.cell_value(i, j), end='\t') print('') Output:Đầu ra sau sẽ xuất hiện sau khi thực thi tập lệnh trên. Ví dụ-2: Đọc tệp Excel bằng OpenPyXLOpenPyXL là một mô -đun Python khác để đọc tệp XLSX và nó cũng không được cài đặt với Python theo mặc định. Chạy lệnh sau từ thiết bị đầu cuối để cài đặt mô -đun này trước khi sử dụng nó.openpyxl is another python module to read the xlsx file, and it is also not installed with Python by default. Run the following command from the terminal to install this module before using it.openpyxl is another python module to read the xlsx file, and it is also not installed with Python by default. Run the following command from the terminal to install this module before using it. Sau khi hoàn thành quá trình cài đặt, hãy tạo tệp Python với tập lệnh sau để đọc tệp sales.xlsx. Giống như mô -đun XLRD, mô -đun OpenPyXL có hàm Load_Workbook () để mở tệp XLSX để đọc. Tệp sales.xlsx được sử dụng làm giá trị đối số của hàm này. Đối tượng của wookbook.active đã được tạo trong tập lệnh để đọc các giá trị của các thuộc tính max_row và max_column. Các thuộc tính này đã được sử dụng trong các vòng lặp để đọc nội dung của tệp sales.xlsx. Hàm phạm vi () đã được sử dụng để đọc các hàng của trang tính và hàm iter_cols () đã được sử dụng để đọc các cột của trang tính. Mỗi trường trong đầu ra sẽ được phân tách bằng hai không gian tab.sales.xlsx file. Like the xlrd module, the openpyxl module has the load_workbook() function to open the xlsx file for reading. The sales.xlsx file is used as the argument value of this function. The object of the wookbook.active has been created in the script to read the values of the max_row and the max_column properties. These properties have been used in the nested for loops to read the content of the sales.xlsx file. The range() function has been used to read the rows of the sheet, and the iter_cols() function has been used to read the columns of the sheet. Each field in the output will be separated by two tab spaces.sales.xlsx file. Like the xlrd module, the openpyxl module has the load_workbook() function to open the xlsx file for reading. The sales.xlsx file is used as the argument value of this function. The object of the wookbook.active has been created in the script to read the values of the max_row and the max_column properties. These properties have been used in the nested for loops to read the content of the sales.xlsx file. The range() function has been used to read the rows of the sheet, and the iter_cols() function has been used to read the columns of the sheet. Each field in the output will be separated by two tab spaces. # Nhập mô -đun OpenYxl Nhập OpenPyxl import openpyxl # Xác định biến để tải wookbook wookbook = openpyxl.load_workbook ("sales.xlsx") wookbook = openpyxl.load_workbook("sales.xlsx") # Xác định biến để đọc tờ Active: Bảng tính = wookbook.Active worksheet = wookbook.active # Lặp lại vòng lặp để đọc các giá trị ô cho i trong phạm vi (0, bảng tính.max_row): & nbsp; & nbsp; bảng tính forcolin.iter_cols (1, bảng tính.max_column): & nbsp; & nbsp; & nbsp; & nbsp; in (col [i] .value, end = "\ t \ t") & nbsp; & nbsp; in('')for i in range(0, worksheet.max_row): forcolin worksheet.iter_cols(1, worksheet.max_column): print(col[i].value, end="\t\t") print('') Output:Đầu ra sau sẽ xuất hiện sau khi thực thi tập lệnh trên. Ví dụ-2: Đọc tệp Excel bằng OpenPyXLOpenPyXL là một mô -đun Python khác để đọc tệp XLSX và nó cũng không được cài đặt với Python theo mặc định. Chạy lệnh sau từ thiết bị đầu cuối để cài đặt mô -đun này trước khi sử dụng nó.pandas from the terminal.pandas from the terminal. Sau khi hoàn thành quá trình cài đặt, hãy tạo tệp Python với tập lệnh sau để đọc tệp sales.xlsx. Giống như mô -đun XLRD, mô -đun OpenPyXL có hàm Load_Workbook () để mở tệp XLSX để đọc. Tệp sales.xlsx được sử dụng làm giá trị đối số của hàm này. Đối tượng của wookbook.active đã được tạo trong tập lệnh để đọc các giá trị của các thuộc tính max_row và max_column. Các thuộc tính này đã được sử dụng trong các vòng lặp để đọc nội dung của tệp sales.xlsx. Hàm phạm vi () đã được sử dụng để đọc các hàng của trang tính và hàm iter_cols () đã được sử dụng để đọc các cột của trang tính. Mỗi trường trong đầu ra sẽ được phân tách bằng hai không gian tab.sales.xlsx file. The read_excel() function of pandas is used for reading the xlsx file. This function has used in the script to read the sales.xlsx file. The DataFrame() function has used here to read the content of the xlsx file in the data frame and store the values in the variable named data. The value of the data has been printed later.sales.xlsx file. The read_excel() function of pandas is used for reading the xlsx file. This function has used in the script to read the sales.xlsx file. The DataFrame() function has used here to read the content of the xlsx file in the data frame and store the values in the variable named data. The value of the data has been printed later. # Nhập Pandas nhập khẩu Pandas dưới dạng PD import pandas as pd # Tải tệp xlsx excel_data = pd.read_excel ('sales.xlsx') '])# In bản in nội dung ("Nội dung của tệp là: \ n", dữ liệu) excel_data = pd.read_excel('sales.xlsx')# Read the values of the file in the dataframe data = pd.DataFrame(excel_data, columns=['Sales Date', 'Sales Person', 'Amount'])# Print the content print("The content of the file is:\n", data) Output:Đầu ra sau sẽ xuất hiện sau khi thực thi tập lệnh trên. Đầu ra của tập lệnh này khác với hai ví dụ trước. Các số hàng được in trong cột đầu tiên, trong đó giá trị hàng đã được tính từ 0. Các giá trị ngày được căn chỉnh theo cách tập trung. Tên của các nhân viên bán hàng được liên kết đúng. Số tiền được căn chỉnh trái. Conclusion:Người dùng Python cần làm việc với các tệp XLSX cho các mục đích lập trình khác nhau. Ba cách khác nhau để đọc tệp XLSX đã được hiển thị trong hướng dẫn này bằng cách sử dụng ba mô -đun Python. Mỗi mô -đun có các hàm và thuộc tính khác nhau để đọc tệp XLSX. Hướng dẫn này sẽ giúp người dùng Python đọc tệp XLSX dễ dàng bằng tập lệnh Python sau khi đọc hướng dẫn này. Thông tin về các Tác giảTôi là một huấn luyện viên của các khóa học lập trình web. Tôi thích viết bài báo hoặc hướng dẫn về các chủ đề CNTT khác nhau. Tôi có một kênh YouTube trong đó nhiều loại hướng dẫn dựa trên Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel, v.v. Làm cách nào để đọc tệp XLSX trong XLRD?Các tệp XLSX thay vì XLRD .... Cài đặt thư viện OpenPyXL trên cụm của bạn .. Xác nhận rằng bạn đang sử dụng Pandas phiên bản 1.0. 1 trở lên. Bản sao Python. Nhập gấu trúc dưới dạng in PD (pd .__ phiên bản__). Chỉ định openpyxl khi đọc.Các tệp XLSX với gấu trúc.Bản sao Python .. Làm cách nào để đọc một tệp XLXS trong Python?Hàm read_excel () của gấu trúc được sử dụng để đọc tệp XLSX.Hàm này đã được sử dụng trong kịch bản để đọc bán hàng.Tệp XLSX.Hàm dataFrame () đã được sử dụng ở đây để đọc nội dung của tệp XLSX trong khung dữ liệu và lưu trữ các giá trị trong biến có tên là dữ liệu.. This function has used in the script to read the sales. xlsx file. The DataFrame() function has used here to read the content of the xlsx file in the data frame and store the values in the variable named data.. This function has used in the script to read the sales. xlsx file. The DataFrame() function has used here to read the content of the xlsx file in the data frame and store the values in the variable named data. Làm cách nào để đọc và viết một tệp excel bằng Python XLRD?Đọc và viết cho tờ Excel trong Python... Introduction.. Đọc dữ liệu từ tờ Excel.Tìm số lượng của các cột và hàng.Trích xuất các giá trị hàng hoặc cột cụ thể.In từng giá trị từ trang tính .. Viết dữ liệu vào Excel.Tạo tiêu đề.Viết dữ liệu .. Hoàn thành mã .. Tại sao XLRD không hỗ trợ XLSX?Bởi vì các phiên bản cũ hơn 1.2.0 sử dụng thư viện XLRD để đọc các tệp Excel.Thư viện XLRD chỉ hỗ trợ đọc.Tệp XLS.0 internally use the xlrd library to read the excel files. The xlrd library supports ONLY reading the . xls files. 0 internally use the xlrd library to read the excel files. The xlrd library supports ONLY reading the . xls files. |