Table of Contents
Application scenario
The effect is as follows
Test data
Source code
Source code description
Home Backend Development Python Tutorial How to modify the metadata of Excel files in Python

How to modify the metadata of Excel files in Python

May 18, 2023 pm 05:46 PM
excel python

Application scenario

This code can be used to modify the metadata of Excel files, such as author, subject, description, etc. By using Python and Openpyxl modules, and the wxPython library, we can create a GUI interface to Enter metadata and save this metadata with the Excel file.

Here are a few possible application scenarios:

Use metadata to more quickly identify and find large numbers of Excel files, especially if they need to be categorized and managed.

Data Sharing: When you need to share an Excel file with others, metadata can provide useful information such as author, subject, and description.

Using metadata allows you to more easily identify and differentiate between multiple Excel files, speeding up the data analysis process.

Data Reporting: When you need to reference an Excel file in a report, metadata can provide useful information such as author, subject, and description.

In short, this code can be used in any scenario where the metadata of an Excel file needs to be modified, from data management to data analysis to data reporting, everything can be achieved through this code.

The effect is as follows

How to modify the metadata of Excel files in Python

Test data

hello
2023-04-18T10:00:00Z
2023-04-17T10:00:00Z
musk
chatgpt
我是一个测试文档
python测试
这是一个运用销售给到用户的应用。
Copy after login

Source code

import os
import wx
from openpyxl import load_workbook
# from openpyxl import __version__ as openpyxl_version
# from openpyxl import DocumentProperties
 
class PropertyEditor(wx.Frame):
    def __init__(self, parent, title):
        super(PropertyEditor, self).__init__(parent, title=title, size=(500, 400))
 
        # 创建GUI界面
        panel = wx.Panel(self)
 
        author_label = wx.StaticText(panel, label="作者:")
        self.author_text = wx.TextCtrl(panel)
 
        created_label = wx.StaticText(panel, label="创建时间:")
        self.created_text = wx.TextCtrl(panel)
 
        modified_label = wx.StaticText(panel, label="修改时间:")
        self.modified_text = wx.TextCtrl(panel)
 
        last_saved_by_label = wx.StaticText(panel, label="最后一次保存者:")
        self.last_saved_by_text = wx.TextCtrl(panel)
 
        computer_label = wx.StaticText(panel, label="计算机:")
        self.computer_text = wx.TextCtrl(panel)
 
        title_label = wx.StaticText(panel, label="标题:")
        self.title_text = wx.TextCtrl(panel)
 
        subject_label = wx.StaticText(panel, label="主题:")
        self.subject_text = wx.TextCtrl(panel)
 
        description_label = wx.StaticText(panel, label="描述:")
        self.description_text = wx.TextCtrl(panel, style=wx.TE_MULTILINE)
 
        save_button = wx.Button(panel, label="保存")
        save_button.Bind(wx.EVT_BUTTON, self.on_save)
 
        # 添加到Sizer中
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(author_label, flag=wx.ALL, border=5)
        sizer.Add(self.author_text, flag=wx.EXPAND|wx.ALL, border=5)
        sizer.Add(created_label, flag=wx.ALL, border=5)
        sizer.Add(self.created_text, flag=wx.EXPAND|wx.ALL, border=5)
        sizer.Add(modified_label, flag=wx.ALL, border=5)
        sizer.Add(self.modified_text, flag=wx.EXPAND|wx.ALL, border=5)
        sizer.Add(last_saved_by_label, flag=wx.ALL, border=5)
        sizer.Add(self.last_saved_by_text, flag=wx.EXPAND|wx.ALL, border=5)
        sizer.Add(computer_label, flag=wx.ALL, border=5)
        sizer.Add(self.computer_text, flag=wx.EXPAND|wx.ALL, border=5)
        sizer.Add(title_label, flag=wx.ALL, border=5)
        sizer.Add(self.title_text, flag=wx.EXPAND|wx.ALL, border=5)
        sizer.Add(subject_label, flag=wx.ALL, border=5)
        sizer.Add(self.subject_text, flag=wx.EXPAND|wx.ALL, border=5)
        sizer.Add(description_label, flag=wx.ALL, border=5)
        sizer.Add(self.description_text, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)
        sizer.Add(save_button, flag=wx.ALL|wx.CENTER, border=10)
 
        panel.SetSizer(sizer)
 
    def on_save(self, event):
        dlg = wx.FileDialog(self, "选择要修改属性的Excel文件", "", "", "*.xlsx", wx.FD_OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            file_path = dlg.GetPath()
 
            try:
                wb = load_workbook(filename=file_path)
 
                # 修改属性
                properties = wb.properties
                properties.creator = self.author_text.GetValue()
                properties.created = self.created_text.GetValue()
                properties.modified = self.modified_text.GetValue()
                properties.lastModifiedBy = self.last_saved_by_text.GetValue()
                properties.computer = self.computer_text.GetValue()
                properties.title = self.title_text.GetValue()
                properties.subject = self.subject_text.GetValue()
                properties.description = self.description_text.GetValue()
 
                wb.save(file_path)
                wx.MessageBox("属性已成功保存!", "提示", wx.OK|wx.ICON_INFORMATION)
 
            except Exception as e:
                wx.MessageBox("修改属性时出错: {}".format(str(e)), "错误", wx.OK|wx.ICON_ERROR)
 
        dlg.Destroy()
 
if __name__ == '__main__':
    app = wx.App()
    frame = PropertyEditor(None, title="修改Excel文件属性")
    frame.Show()
    app.MainLoop()
Copy after login

Source code description

With the help of the GUI interface built by the wxPython module, users can enter the attributes of the Excel file that need to be modified. Users can enter the author, creation time, modification time, title, subject and description, and then click the "Save" button to save these properties and write them into the corresponding properties of the Excel file.

This code creates a wxPython window named PropertyEditor, which contains text boxes for entering Excel file properties and a "Save" button. When the "Save" button is clicked, it will get the Excel file that the user wants to modify and save the entered property values ​​into the properties of the Excel file. It then displays a message box prompting the user that the save was successful.

Please note that this program assumes that the user already knows the path to the modified Excel file. Using the wxPython module's file dialog box, users can browse the file system to select Excel files.

The above is the detailed content of How to modify the metadata of Excel files in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

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.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

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.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

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 and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

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.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

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.

See all articles