首先需要导入第三方库
pip install openpyxl
这个库还是比较强大的。
接下来主要隐藏行是
sheet.row_dimensions.group(start_row,end_row,hidden=True)
隐藏列
sheet.column_dimensions.group(start_column,end_column,hidden=True)
值得注意的地方是行的起始时数字,如5至6,列是英文字母。下面附上完整实例供大家参考。
import openpyxl
from openpyxl import load_workbook
import re
wb = load_workbook("group.xlsx",data_only=True)
ws = wb.active
ws = wb['累计完成比月累计计划 (出口) ']
num_cols = ws.max_column
col_arr = []
no_num = []
# 累计完成. 比月度累计计划 比年度计划进度 同比
for i in range(2,num_cols+1):
tt = ws.cell(3,i).value
col_name = re.match(r'\D+',ws.cell(3,i).coordinate).group()
if tt != None and tt != '累计完成' and tt != '比月度累计计划' and tt != '比年度计划进度' and tt != '同比':
no_num.append(col_name)
col_arr.append(col_name)
for i in range(len(no_num)):
for j in range(len(col_arr)):
if no_num[i] == col_arr[j]:
ws.column_dimensions.group(col_arr[j],col_arr[j+4],hidden=True)
ws.row_dimensions.group(6,7,hidden=True)
wb.save("print-group.xlsx")