Operations 7 min read

Automating Sales Daily Reports with Python: A Step‑by‑Step Guide

This article demonstrates how to use Python, pandas, and python‑docx to read sales data from Excel, compute necessary metrics, and automatically generate formatted Word daily‑report documents in bulk, turning a repetitive manual task into an efficient automated workflow.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Automating Sales Daily Reports with Python: A Step‑by‑Step Guide

Daily reports are essential for team management but can become a tedious repetitive task; the article proposes automating this process with Python by assembling a report template and letting Python fill in the variable data.

The requirement is to read daily sales figures stored in Excel, process them, and write the results into a Word document, enabling batch generation of reports for many days.

Data processing is performed with pandas: the script imports pandas, reads the Excel file, formats dates, calculates daily completion rates, cumulative sales, annual completion percentages, and scales values as needed.

import pandas as pd df = pd.read_excel("日报数据.xlsx") df["日期"] = df["日期"].apply(lambda x: x.strftime("%Y-%m-%d")) df["当日完成度"] = (df["销售金额"]/df["销售目标"]*100).round(1) df["累计销售金额"] = df["销售金额"].cumsum() df["当年完成度"] = (df["累计销售金额"]/2200000*100).round(1) df["累计销售金额"] = (df["累计销售金额"]/10000).round(2) df

After processing, the script uses the python‑docx library to open a Word template and replace placeholders with the computed values. It iterates over the DataFrame rows, updates specific paragraph runs, and fills a table with the past seven days of data.

for index, rows in df.iterrows(): if index > 30: doc.paragraphs[0].runs[1].text = rows[0] doc.paragraphs[4].runs[4].text = rows[0] doc.paragraphs[4].runs[6].text = str(rows[1]) doc.paragraphs[4].runs[8].text = str(rows[2]) doc.paragraphs[5].runs[1].text = str(rows[3]) doc.paragraphs[5].runs[3].text = str(rows[4]) doc.paragraphs[9].runs[2].text = str(rows[5]) doc.paragraphs[9].runs[7].text = str(rows[6]) table = doc.tables[0] data_table = df.iloc[index-6:index+1, :5] for i in range(7): for j in range(5): table.cell(i+1, j).text = str(df.iloc[i, j]) doc.save(f"销售日报-{rows[0]}.docx")

Running the script produces a batch of Word files—120 sales daily reports in the example—demonstrating the power of Python for office automation.

The article concludes that Python’s simplicity makes it ideal for beginners to automate repetitive computer tasks, and suggests alternatives such as the docxtpl library with Jinja2 for more flexible template handling.

PythonautomationData Processingscriptingpandaspython-docxdaily-report
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.