CodeMan

Each of us must find a way to love the world. You have found yours.

0%

Python处理Excel文件Day3之转换某列数据大小写

问题描述:

需要把excel中的某一列做转换大小写操作,具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# coding=utf-8

from openpyxl import load_workbook


def change_col_to_upper_or_lower(file_path='',
sheet='',
column='',
to_upper=True,
):
wb = load_workbook(file_path)
ws = wb[sheet]
max_row = ws.max_row
min_row = ws.min_row

for row_of_cell_objects in ws[column + str(min_row):column + str(max_row)]:
for cell_obj in row_of_cell_objects:
if to_upper:
cell_obj.value = cell_obj.value.upper()
else:
cell_obj.value = cell_obj.value.lower()
wb.save(file_path)

change_col_to_upper_or_lower('col_copy_tracking.xlsx','control','I')