Automatically generate data daily reports with Python!
In fact, I think it is quite simple. The core is that you assemble the content template of the daily report, and then give the changed amount to python to fill in. What you need is basically python to process excel and word Libraries related to ppt and so on. Use them skillfully and you can automate a whole process.
Daily newspapers are a problem that most migrant workers cannot avoid.
For managers, daily reports are the best way to manage beforehand and understand the atmosphere and status of the team. But for employees, there is nothing to talk about. For repetitive work, I highly recommend everyone to use Python to modularize and automate it, helping us achieve efficient office work.
Let’s demonstrate the advantages of Python’s automated office through a case of supplementing a sales daily report. This article simplifies the process of the case, and the complete code is attached at the end of the article.
Detailed explanation of requirements
My friend’s needs are as follows. Their usual sales data are recorded in Excel, and after summary, statistics will be made by department. But at the beginning of this year, the leader suddenly asked us to write a daily report. After writing for a month, we found that we did not check it and stopped writing.
Now I am suddenly required to submit all the daily reports before this month tomorrow. This is equivalent to making up nearly 120 days of daily reports from February to May. If you copy and paste with both hands, then Probably vomiting blood. A friend sent over all the relevant documents for writing his daily report and found that the final effect of the daily report is as follows.
So the requirements are relatively simple. You only need to read the daily data from the Excel table, use Python to process it, and then write it into the Word document. , you can generate daily reports in batches.
Data processing
Before performing data processing, you must first understand what data is ultimately needed. As shown in the figure below, the target daily report in Word is mainly divided into two categories: the values marked in red are mainly composed of the data of the day, or the data obtained after their calculation; the table marked in green is simpler, that is, the past seven days data (sales quantity, sales amount, sales target, degree of completion).
First we import the Pandas module for data processing.
import pandas as pd df = pd.read_excel("日报数据.xlsx") df
Output result:
After importing the data, we can then perform data operations according to our needs. Data operations are mainly divided into two types, one is to use addition, subtraction -, multiplication *, and division / to perform data operations, and the other is to use statistical methods to perform data operations.
Enter the following command in the interactive environment:
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
Output result:
As you can see, the final result is marked in red in the screenshot The data contents have all been calculated. The table marked in green is even simpler, just use the data selection in the Pandas module.
Enter the following command in the interactive environment:
num = 10 df.iloc[num-7:num, :5]
Output result:
You can easily get a certain A collection of daily data within the past 7 days of the date.
Automatically generate daily reports
Using Python to automatically operate Word usually uses the python-docx module, and there are generally two methods for batch generating Word documents: using add_ paragraph(), add_table() and other methods Add various content to Word documents. The other one is what we are going to use this time, which is to replace text and table data in the original Word document according to position.
Enter the following command in the interactive environment:
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")
Execute the code and output the result:
As shown in the figure above, 120 copies The recorded sales daily report is ready. Python automated office is so magical.
How to obtain the complete code:
Link:https://www.php.cn/link/0d5a4a5a748611231b945d28436b8ece
Extraction code: p9iw
Because of its simple syntax and easy to use, Python is called the "most suitable programming language for beginners to learn". For various repetitive computer tasks at work, you can consider using Python to transform them into automated programs.
If you are a Python beginner, you will find that the logic of this article is very simple, and you can even improve it. For example, the python-docx module has advantages in reading Word documents, but when writing text to templates, you can consider using the docxtpl module (learn a little Jinja2 syntax).
The above is the detailed content of Automatically generate data daily reports with Python!. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

The key to running Jupyter Notebook in VS Code is to ensure that the Python environment is properly configured, understand that the code execution order is consistent with the cell order, and be aware of large files or external libraries that may affect performance. The code completion and debugging functions provided by VS Code can greatly improve coding efficiency and reduce errors.
