def indent_col(header, indent_col_no,target_col,sleep_time):
wb = xw.books.active
sht = xw.sheets.active
# active_cell = wb.selection # 也可以写成:wb.App.selection
rng_current = wb.selection.current_region # 上一行不要删,留作参考
rng_used = sht.used_range
row_count = rng_used.rows.count
col_count = rng_used.columns.count
rng_current_start_row = rng_current.row
rng_current_start_col = rng_current.column
rng_current_last_row = rng_current.row + (row_count - 1)
rng_current_last_col = rng_current.column + (col_count - 1)
scol = rng_current_start_col
ecol = rng_current_last_col
srow = rng_current_start_row + header
erow = rng_current_last_row
for i in range(srow,erow + 1):
if indent_col_no > 0:
indent_value = int(sht.range(i,indent_col_no).value) # 要int一下,否则是float数
if indent_value == 0:
target_cell = sht.range(i,index_col)
target_cell.api.IndentLevel = indent_value
sht.range(i,index_col).api.HorizontalAlignment = left_alignment
sht.range(i,index_col).api.Font.Bold = True
time.sleep(sleep_time)
else:
sht.range(i,index_col).api.IndentLevel = indent_value
sht.range(i,index_col).api.HorizontalAlignment = left_alignment
time.sleep(sleep_time)
def zebra_stripes(header,first_line_fill_color,second_line_fill_color,sleep_time):
wb = xw.books.active
sht = xw.sheets.active
active_cell = wb.selection # 也可以写成:wb.App.selection
rng_current = wb.selection.current_region # 上一行不要删,留作参考
rows_count = rng_current.rows.count
columns_count = rng_current.columns.count
# get 4 cornors
rng_current_start_row = rng_current.row
rng_current_start_column = rng_current.column
rng_current_last_row = rng_current.row + (rows_count - 1)
rng_current_last_column = rng_current.column + (columns_count - 1)
# 隔行设置单元格背景色
for j in range((rng_current_start_row + header), (rng_current_last_row + 1), 2):
# 步进为2,一行隔一行;最后一行要+1 - Python 括号前闭后开
x1 = j
y1 = rng_current_start_column
x2 = j
y2 = rng_current_last_column
sht.range((x1,y1),(x2,y2)).color = first_line_fill_color # 一行灰色
time.sleep(sleep_time)
x3 = j + 1
y3 = rng_current_start_column
x4 = j + 1
y4 = rng_current_last_column
sht.range((x3,y3),(x4,y4)).color = second_line_fill_color # 下一行白色
time.sleep(sleep_time)
还是根据xlwings包来做的,感觉能够比较快的提高效率