1. 遍历文件夹中的路径+文件名
import glob
import os
filelocation="D:\\temp\\"
filenames = os.listdir(filelocation) #获取文件名
filepath = [i for i in glob.glob(filelocation+'*') ]
filepath
>>>
['D:\\temp\\201',
'D:\\temp\\571 NYC',
'D:\\temp\\color_test.xlsx',
'D:\\temp\\C_TEMP',
'D:\\temp\\DataFrame.md',
'D:\\temp\\iterrows.jpg',
'D:\\temp\\R_描述统计temp.md',
'D:\\temp\\sift',
'D:\\temp\\task3.csv',
'D:\\temp\\test.xlsx',
'D:\\temp\\tingting2 表join并去重',
'D:\\temp\\____tingting-表金额凑数',
'D:\\temp\\____typroa-有道云 改数学公式',
'D:\\temp\\批注 2020-05-13 103717.jpg']
2. 遍历excel中不同的sheet
import openpyxl
filepath = ' '
wb = openpyxl.load_workbook(filepath) # 目标表
sheet_names = wb.get_sheet_names() # 获取sheet名字
sheet = wb.get_sheet_by_name(sheet_names[0]) # 获取sheet内容
'''
while f < len(sheet_names):
sheet = wb.get_sheet_by_name(sheet_names[f])
nrows = sheet.max_row # 获取sheet中最大行数
ncols = sheet.max_column
for m in range(1, nrows+1):
for n in range(1,ncols+1):
s_hex = sheet.cell(row = m, column = n).fill.start_color.index
hex_data.append(s_hex)
f+=1
'''
3. 遍历excel单元格
3.1 第一种方案:df.iterrows()
import pandas as pd
for index, row in df.iterrows():
# print(index, row)
print(row['A']) # 按列取 (列名),打印A列的所有值
# 或
for row in df.iterrows():
print(row[1][0]) # df第一列的内容
# row[0]-->df的index; row[1]--> df的内容
3.2 第二种方案:iloc
iloc + range
for i in range(0, len(df)): # len(df) 行数
for k in df.iloc[i]: # df.iloc[i]按列获取
print(k) # 每个单元格内容
iloc[].values
value = df.iloc[0:,0:].values # 读几列取第几列的值(这里是获取全表
for arr in value:
for row in arr:
print(row)
3.3 第三种方案:sheet.cell.value
- 按行获取每个单元格的内容
- 适用
- 已知单元格坐标,查询坐标的值
- 查询excel原始公式(这种方法会返回excel中的原始公式
import openpyxl
wb = openpyxl.load_workbook(filepath)
sheet_names = wb.get_sheet_names()
sheet = wb.get_sheet_by_name(sheet_names[0]) # 获取第一张sheet
nrows = sheet.max_row # 获取sheet中最大行数
ncols = sheet.max_column
for m in range(1, nrows+1): # 定位行列坐标
for n in range(1,ncols+1):
print(sheet.cell(row = m, column = n).value) # sheet.cell.value 获取值
>>>
A
B
C
D
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()
=RAND()