Automate ANY Excel Design Sheet Using python’s openpyxl.pdf
1.
2. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
3.
4.
5.
6. Input Cells
Cell Description
Q14 Project Name
Q16 Item Name
U16 ID
C4 Lb_in
C5 Lb_out
C6 L_LTB
C7 Cb
C10 Type
C11 Rolled Profile
A26 h
A27 b
A28 tf
A29 tw
C14 Pu
C15 Mux
C16 Muy
C17 Vu
C20 fy
C21 fu
C22 E
Output Cells
Cell Description
U19 Compactness
U20 Compression
U21 Flexure
U22 Shear
U23 Interaction
K27 P Utilization
O24 Mx Utilization
G6 Section Area
7.
8.
9. 1. Prepare Workbook with Template and Multi-input Sheets
2. Open Workbook → wb
3. Open Multi-input Sheet → multi_input_sheet
4. Open Template Sheet → template_sheet
5. Get input/output cell titles (row 2 in multi_input_sheet) → input_titles; output_titles
6. Foreach data_row in multi_input_sheet:
7. Create Copy of template_sheet → iter_sheet where iter_sheet.title equals data_row.id
8. Get input cells in row → input_cells
9. Get output cells in row → output_cells
10. Foreach input_cell in input_cells:
11. iter_sheet.cell(idx_of(input_title)).value equals "='Multi Input'!" + idx_of(input_cell)
12. Foreach output_cell in output_cells:
13. output_cell.value equals "=" + data_row.id + "'!" + output_title
14. Save wb
15. Close wb
10. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
11. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
12. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
13. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
14. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
15. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
16. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
17. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
18. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
19. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)
20. from openpyxl import *
sheet_path, input_end, output_end, non_temp_sheets = file.xlsx", 'U', 'AB', 4
def col2num(col):
num = 0
for c in col:
if c in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ":
num = num * 26 + (ord(c.upper()) - (ord('A')-1))
return num
def make_sheets(sheet_path, input_end, output_end, non_temp_sheets):
wb = load_workbook(sheet_path)
numeric_input_end = col2num(input_end)
numeric_output_end = col2num(output_end)
template_sheet = wb["Template"]
multi_input_sheet = wb["Multi Input"]
# removing excess sheets from previous runs
for name in wb.sheetnames[non_temp_sheets:]:
wb.remove(wb[name])
rows = [_ for _ in multi_input_sheet.iter_rows()]
for row in rows[3:]:
row_id = str(row[2].value)
if row_id == "None": continue
# make a copy of the template & name the sheet with the id
iter_sheet = wb.copy_worksheet(template_sheet)
iter_sheet.title = row_id
print("Processing member ID: " + row_id)
# inputting
for input_cell in row[:numeric_input_end]:
iter_sheet[rows[1][input_cell.column - 1].value] = "='Multi Input'!" + utils.cell.get_column_letter(input_cell.column) + str(input_cell.row)
# outputting
for output_cell in row[numeric_input_end:numeric_output_end]:
output_cell.value = "='" + iter_sheet.title + "'!" + rows[1][output_cell.column - 1].value
print("Saving...")
wb.save(sheet_path)
wb.close()
print("Done.")
make_sheets(sheet_path, input_end, output_end, non_temp_sheets)