Automate Payroll Generation with Python: Step-by-Step Excel Script
Learn how to replace repetitive Excel payroll creation with a Python script that reads, copies, styles, and inserts rows, updates formulas, and outputs a ready-to-use payroll sheet, complete with full code examples and detailed explanations.
What the article solves
A friend asked for a way to automate the monthly payroll generation that was previously done manually in Excel. The article shows a Python script that can read the original Excel file, duplicate a sheet, copy styles, insert rows, and update formulas to produce a ready‑to‑use payroll sheet.
The original data layout is displayed, followed by the expected final payroll format.
Code Logic Analysis
The program first reads the Excel workbook, copies the source sheet, and creates a new sheet named final_工资条. It then extracts the header row and the style of the first cell (alignment, border, fill, font) using a helper function.
import re
import openpyxl
from copy import copy
wb = openpyxl.load_workbook('工资条.xlsx')
wb.copy_worksheet(wb['工资条'])
ws = wb.worksheets[-1]
ws.title = 'final_工资条'For each data row, the script inserts two rows beneath it, writes the header with the previously captured style, and updates any formulas that reference column H or J by adjusting the row number with a regular‑expression substitution.
def cell_style(cell):
alignment = copy(cell.alignment) # 对齐样式
border = copy(cell.border) # 边框样式
fill = copy(cell.fill) # 填充样式
font = copy(cell.font) # 字体样式
return alignment, border, fill, font
# ... loop that inserts rows and updates formulas ...Full Script
import re
import openpyxl
from copy import copy
def cell_style(cell):
alignment = copy(cell.alignment) # 对齐样式
border = copy(cell.border) # 边框样式
fill = copy(cell.fill) # 填充样式
font = copy(cell.font) # 字体样式
return alignment, border, fill, font
wb = openpyxl.load_workbook('工资条.xlsx')
wb.copy_worksheet(wb['工资条'])
ws = wb.worksheets[-1]
ws.title = 'final_工资条'
cells_rows = [[cell for cell in row] for row in ws.rows]
header = [cell.value for cell in cells_rows[0]]
alignment, border, fill, font = cell_style(cell=cells_rows[0][0])
for i, _ in enumerate(cells_rows[1:]):
if i > 0:
index = i * 3
ws.insert_rows(idx=index, amount=2)
for j, v in enumerate(header):
r, c = index + 1, j + 1
cell = ws.cell(row=r, column=c)
cell.value = v
cell.alignment = alignment
cell.font = font
cell.border = border
cell.fill = fill
if cell.coordinate[:1] in ('H', 'J'):
cell = ws.cell(row=r+1, column=c)
cell.value = re.sub(r'\d+', str(r+1), cell.value)
wb.save('工资条.xlsx')Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
