openpyxl을 사용하여 .xlsx 시트에 있는 행과 열의 수를 가져오는 방법이 있습니까?
openpyxl을 사용하여 .xlsx 시트에 있는 행과 열의 수를 가져오는 방법이 있습니까?xlrd에서,
sheet.ncols
sheet.nrows
열 및 행 개수를 제공합니다.openpyxl에 그런 방법이 있습니까?
주어진 변수sheet행 및 열 수 결정은 다음 방법 중 하나로 수행할 수 있습니다.
버전 ~= 3.0.5 구문
rows = sheet.max_rows
columns = sheet.max_column
버전 1.x.x 구문
rows = sheet.nrows
columns = sheet.ncols
버전 0.x.x 구문
rows = sheet.max_row
columns = sheet.max_column
Dani의 솔루션을 기반으로 하고 있으며 거기에 언급할 충분한 평판을 가지고 있지 않습니다.검색에 소요되는 시간을 줄이기 위해 수동 컨트롤을 추가하여 코드를 편집했습니다.
## iteration to find the last row with values in it
nrows = ws.max_row
if nrows > 1000:
nrows = 1000
lastrow = 0
while True:
if ws.cell(nrows, 3).value != None:
lastrow = nrows
break
else:
nrows -= 1
워크시트의 메서드는 'dim_colmax', 'dim_colmin', 'dim_rowmax', 'dim_rowmin'입니다.
다음은 작은 예입니다.
import pandas as pd
writer = pd.ExcelWriter("some_excel.xlsx", engine='xlsxwriter')
workbook = writer.book
worksheet = writer.sheets[RESULTS_SHEET_NAME]
last_row = worksheet.dim_rowmax
이것이 논리입니다.
number_of_rows = sheet_obj.max_row
last_row_index_with_data = 0
while True:
if sheet_obj.cell(number_of_rows, 3).value != None:
last_row_index_with_data = number_of_rows
break
else:
number_of_rows -= 1
Pandas를 사용하여 모든 시트 행 및 열 개수를 가져오는 솔루션입니다.그것은 사용합니다.df.shape카운트를 받기 위해.
import pandas as pd
xl = pd.ExcelFile('file.xlsx')
sheetnames = xl.sheet_names # get sheetnames
for sheet in sheetnames:
df = xl.parse(sheet)
dimensions = df.shape
print('sheetname', ' --> ', sheet)
print(f'row count on "{sheet}" is {dimensions[0]}')
print(f'column count on "{sheet}" is {dimensions[1]}')
print('-----------------------------')
해라
import xlrd
location = ("Filelocation\filename.xlsx")
wb = xlrd.open_workbook(location)
s1 = wb.sheet_by_index(0)
s1.cell_value(0,0) #initializing cell from the cell position
print(" No. of rows: ", s1.nrows)
print(" No. of columns: ", s1.ncols)
비어 있지 않은 열 수가 필요할 때는 전체 열 수가 아니라 비어 있지 않은 열 수를 주의하십시오라는 것이 더 효율적입니다.더 효율적이라는 말은 목표를 달성하기 위한 가장 쉬운 방법을 의미하지만 가장 빠른 방법은 아닙니다(실행 속도를 테스트하지 않았습니다).다음에,sheet의 예입니다.openpyxl.worksheet.worksheet.Worksheet:
values = list(sheet.values) #values is a list of tuple of same len
nb_cols = len(values[0])
비어 있지 않은 줄의 수가 필요한 경우 다음 작업을 수행합니다.
nb_lines = len([v for v in sheet.values if any(v)])
이 마지막 명령은 실패할 수 있습니다. 행이 0만 있으면 빈 것으로 간주됩니다.
언급URL : https://stackoverflow.com/questions/35408339/is-there-any-method-to-get-the-number-of-rows-and-columns-present-in-xlsx-sheet
'programing' 카테고리의 다른 글
| Spring Scheduled 주석에서 고정 속도와 고정 지연의 차이점은 무엇입니까? (0) | 2023.08.12 |
|---|---|
| 당신은 PHP의 클래스 내에서 정적 상수를 사용할 수 있습니까? (0) | 2023.08.12 |
| Oracle 날짜를 문자열로 변환 (0) | 2023.08.12 |
| Amazon S3 boto - 폴더를 삭제하는 방법? (0) | 2023.08.12 |
| CSS: 창 너비의 50%에 해당하는 배경색을 설정합니다. (0) | 2023.08.12 |