search
HomeBackend DevelopmentPython TutorialAutomatically generate data daily reports with Python!

Automatically generate data daily reports with Python!

May 25, 2023 pm 05:01 PM
pythonOrderData Daily

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.

Automatically generate data daily reports with Python!

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.

Automatically generate data daily reports with Python!

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.

Automatically generate data daily reports with Python!

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).

Automatically generate data daily reports with Python!

First we import the Pandas module for data processing.

import pandas as pd
df = pd.read_excel("日报数据.xlsx")
df

Output result:

Automatically generate data daily reports with Python!

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:

Automatically generate data daily reports with Python!

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:

Automatically generate data daily reports with Python!

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:

Automatically generate data daily reports with Python!

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!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment