我在一个选项卡中有几列。我通过
wb_master_sheet.column_dimensions["A"].width = 18
设置第一个
在第一个for f in xlsx_files:
循环中,其余的是width = 3
。如何编写for
或while
循环来检查从'B'
到'AG'
的范围并将width
设置为3?
from openpyxl import load_workbook, Workbook, utils
wb_master = Workbook()
wb_01 = './tmp/users_general_inf.xlsx'
wb_02 = './tmp/users_dates_inf.xlsx'
wb_03 = './tmp/users_controls_inf.xlsx'
xlsx_files = [wb_01, wb_02, wb_03]
for f in xlsx_files:
sub_wb = load_workbook(f)
sub_ws = sub_wb.active
wb_master_sheet = wb_master.create_sheet(sub_ws.title)
for row in sub_ws.iter_rows():
for cell in row:
wb_master_sheet[cell.coordinate].value = cell.value
wb_master_sheet.column_dimensions["A"].width = 18
del wb_master['Sheet']
# ------ set for userAccountControl sheet
sheet_3_name = 'userAccountControl'
flag_col_width = 3
wb_master[sheet_3_name].column_dimensions['B'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['C'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['D'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['E'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['F'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['G'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['H'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['I'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['J'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['K'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['L'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['M'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['N'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['O'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['P'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['Q'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['R'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['S'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['T'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['U'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['V'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['W'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['X'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['Y'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['Z'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['AA'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['AB'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['AC'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['AD'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['AE'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['AF'].width = flag_col_width
wb_master[sheet_3_name].column_dimensions['AG'].width = flag_col_width
# ------ saving the master excel workbook
wb_master.save('./reports/master_users_inf.xlsx')
您可以首先在 Excel 中列出所有可能的列字母(
'A'
到 'XFD'
),然后根据您需要为其设置宽度的列范围制作切片/循环。
openpyxl.utils.cell.get_column_letter
:
from openpyxl.utils.cell import get_column_letter
col_xl = [get_column_letter(idx) for idx in range(1, 16384+1)] #16384 is Excel's limit
start_col, end_col = ("B", "AG")
sheet_3_name = 'userAccountControl'
flag_col_width = 3
for col in col_xl[col_xl.index(start_col):col_xl.index(end_col)+1]:
wb_master[sheet_3_name].column_dimensions[col].width = flag_col_width