Tôi chỉ muốn kéo cột A từ bảng tính của tôi. Tôi có mã dưới đây, nhưng nó lấy từ tất cả các cột. from openpyxl import Workbook, load_workbook
wb=load_workbook("/home/ilissa/Documents/AnacondaFiles/AZ_Palmetto_MUSC_searchterms.xlsx", use_iterators=True)
sheet_ranges=wb['PrivAlert Terms']
for row in sheet_ranges.iter_rows(row_offset=1):
for cell in row:
print(cell.value)
Simhumileco 28.8K16 Huy hiệu vàng127 Huy hiệu bạc106 Huy hiệu đồng16 gold badges127 silver badges106 bronze badges Hỏi ngày 12 tháng 1 năm 2016 lúc 21:26Jan 12, 2016 at 21:26
Đây là một giải pháp thay thế cho các câu trả lời trước đó trong trường hợp bạn đọc một hoặc nhiều cột bằng cách sử dụng openpyxl import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
Tôi hy vọng rằng điều này là hữu ích. Đã trả lời ngày 13 tháng 10 năm 2016 lúc 17:23Oct 13, 2016 at 17:23 ZlnkzlnkZLNK 77111 Huy hiệu bạc16 Huy hiệu đồng11 silver badges16 bronze badges 4 Sử dụng OpenPyXL from openpyxl import load_workbook
# The source xlsx file is named as source.xlsx
wb=load_workbook("source.xlsx")
ws = wb.active
first_column = ws['A']
# Print the contents
for x in xrange(len(first_column)):
print(first_column[x].value)
Đã trả lời ngày 16 tháng 3 năm 2017 lúc 2:53Mar 16, 2017 at 2:53
1 Theo tôi đơn giản hơn nhiều from openpyxl import Workbook, load_workbook
wb = load_workbook("your excel file")
source = wb["name of the sheet"]
for cell in source['A']:
print(cell.value)
Đã trả lời ngày 5 tháng 12 năm 2018 lúc 15:15Dec 5, 2018 at 15:15 LorenzolorenzoLorenzo Phim huy hiệu bạc 1711 Huy hiệu đồng1 silver badge4 bronze badges 1 Tôi sẽ đề nghị sử dụng thư viện Pandas. import pandas as pd
dataFrame = pd.read_excel("/home/ilissa/Documents/AnacondaFiles/AZ_Palmetto_MUSC_searchterms.xlsx", sheetname = "PrivAlert Terms", parse_cols = 0)
Nếu bạn không cảm thấy thoải mái trong gấu trúc hoặc vì bất kỳ lý do gì cần phải làm việc với OpenPyXL, thì lỗi trong mã của bạn là bạn không chỉ chọn cột đầu tiên. Bạn gọi rõ ràng cho mỗi ô trong mỗi hàng. Nếu bạn chỉ muốn cột đầu tiên, thì chỉ nhận được cột đầu tiên trong mỗi hàng. for row in sheet_ranges.iter_rows(row_offset=1):
print(row[0].value)
Đã trả lời ngày 12 tháng 1 năm 2016 lúc 22:19Jan 12, 2016 at 22:19 ThtuthtuThtu 1.89214 huy hiệu bạc21 Huy hiệu đồng14 silver badges21 bronze badges 1 Sử dụng import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
3 để kiểm soát chính xác phạm vi của các ô, chẳng hạn như một cột duy nhất, được trả về. Đã trả lời ngày 13 tháng 1 năm 2016 lúc 8:25Jan 13, 2016 at 8:25 Charlie Clarkcharlie ClarkCharlie Clark 17.4K4 Huy hiệu vàng45 Huy hiệu bạc52 Huy hiệu đồng4 gold badges45 silver badges52 bronze badges 1 Đây là một chức năng đơn giản: import openpyxl
def return_column_from_excel(file_name, sheet_name, column_num, first_data_row=1):
wb = openpyxl.load_workbook(filename=file_name)
ws = wb.get_sheet_by_name(sheet_name)
min_col, min_row, max_col, max_row = (column_num, first_data_row, column_num, ws.max_row)
return ws.get_squared_range(min_col, min_row, max_col, max_row)
Đã trả lời ngày 5 tháng 7 năm 2016 lúc 15:03Jul 5, 2016 at 15:03 CompadrecompadreCompadre 7659 Huy hiệu bạc18 Huy hiệu đồng9 silver badges18 bronze badges
Bằng cách sử dụng Thư viện OpenPyXL và khái niệm danh sách của Python: import openpyxl
book = openpyxl.load_workbook('testfile.xlsx')
user_data = book.get_sheet_by_name(str(sheet_name))
print([str(user_data[x][0].value) for x in range(1,user_data.max_row)])
Đó là cách tiếp cận khá tuyệt vời và đáng để thử Đã trả lời ngày 24 tháng 3 năm 2017 lúc 8:49Mar 24, 2017 at 8:49
Sử dụng phản hồi tuyệt vời của ZLNK, tôi đã tạo chức năng này sử dụng khả năng hiểu danh sách để đạt được kết quả tương tự trong một dòng duy nhất: def read_column(ws, begin, columns):
return [ws["{}{}".format(column, row)].value for row in range(begin, len(ws.rows) + 1) for column in columns]
Sau đó, bạn có thể gọi nó bằng cách chuyển một bảng tính, một hàng để bắt đầu và chữ cái đầu tiên của bất kỳ cột nào bạn muốn trả lại: column_a_values = read_column(worksheet, 2, 'A')
Để trả về cột A và cột B, cuộc gọi thay đổi thành này: import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
0 Đã trả lời ngày 4 tháng 3 năm 2017 lúc 18:21Mar 4, 2017 at 18:21 Ewilanewilanewilan
6186 Huy hiệu bạc15 Huy hiệu Đồng6 silver badges15 bronze badges 2 Tôi biết tôi có thể đến trễ tham gia để trả lời chủ đề này. Nhưng ít nhất câu trả lời của tôi có thể từ chối người khác có thể đang tìm cách giải quyết. Bạn phải lặp lại thông qua các giá trị cột của trang tính. Theo ý kiến của tôi, người ta có thể thực hiện như thế này: import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
1import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
4 Vòng lặp qua các hàng của các cột được chỉ định. Bạn có thể chỉ định các đối số của import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
4 từ MIN_ROW đến import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
6 và cả import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
7. Cài đặt import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
8 Ở đây làm cho nó lặp qua tất cả các hàng của cột (cột lên đến mức tối đa được chỉ định). Điều này kéo tất cả các giá trị của FirstColumn của bảng tính của bạnTương tự nếu bạn muốn lặp qua tất cả các cột của một hàng, đó là theo hướng ngang, thì bạn có thể sử dụng import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
9 Chỉ định các thuộc tính từ hàng và cho đến khi Đã trả lời ngày 17 tháng 11 năm 2021 lúc 14:37Nov 17, 2021 at 14:37 PriyapriyaPriya Huy hiệu đồng bạc 1991 Silver131 silver badge13 bronze badges
0 Cập nhật câu trả lời từ phản hồi của ZLNK: import openpyxl
wb = openpyxl.load_workbook('origin.xlsx')
first_sheet = wb.get_sheet_names()[0]
worksheet = wb.get_sheet_by_name(first_sheet)
#here you iterate over the rows in the specific column
for row in range(2,worksheet.max_row+1):
for column in "ADEF": #Here you can add or reduce the columns
cell_name = "{}{}".format(column, row)
worksheet[cell_name].value # the value of the specific cell
... your tasks...
2 Đã trả lời ngày 4 tháng 1 lúc 12:26Jan 4 at 12:26
Làm cách nào để đọc một cột cụ thể trong Excel trong Python?
Làm cách nào để trích xuất các cột cụ thể trong Excel bằng Python ?.. Nhập Gandas dưới dạng PD .. Nhập Numpy dưới dạng NP .. file_loc = "path.xlsx". df = pd. read_excel (file_loc, index_col = none, na_values = ['na'], usecols = "a, c: aa"). print(df).
Làm thế nào để bạn truy cập một cột cụ thể trong openpyxl?
get_sheet_by_name (first_sheet){} ".định dạng (cột, hàng) bảng tính [cell_name].Giá trị # Giá trị của ô cụ thể ... #here you iterate over the rows in the specific column for row in range(2,worksheet. max_row+1): for column in "ADEF": #Here you can add or reduce the columns cell_name = "{}{}". format(column, row) worksheet[cell_name]. value # the value of the specific cell ...
Làm cách nào để đọc các ô trong openpyxl?
OpenPyxl đọc dữ liệu từ ô.. nhập openpyxl .. wb = openpyxl.load_workbook ('sample_file.xlsx'). Sheet = WB.Active .. x1 = tờ ['A1']. x2 = tờ ['A2']. #sử dụng chức năng CELL () .. x3 = sheet.cell (hàng = 3, cột = 1). in ("Giá trị ô đầu tiên:", x1.value).
Làm cách nào để nhận được các giá trị hàng trong openpyxl?
Thuật toán (các bước) tạo một biến để lưu trữ đường dẫn của tệp excel đầu vào.Để tạo/tải sổ làm việc, hãy truyền tệp Excel đầu vào dưới dạng đối số cho hàm Load_Workbook () của mô -đun OpenPyXL (tải một sổ làm việc).Truy cập bảng cụ thể của sổ làm việc bằng cách đặt tên trang này làm chỉ mục cho đối tượng sổ làm việc.Create a variable to store the path of the input excel file. To create/load a workbook, pass the input excel file as an argument to the openpyxl module's load_workbook() function (loads a workbook). Access the specific sheet of the workbook by giving the sheet name as the index to the workbook object. |