search
HomeBackend DevelopmentPython TutorialHow to modify the metadata of Excel files in 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测试
这是一个运用销售给到用户的应用。

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

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
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
What are some common operations that can be performed on Python arrays?What are some common operations that can be performed on Python arrays?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

In what types of applications are NumPy arrays commonly used?In what types of applications are NumPy arrays commonly used?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

When would you choose to use an array over a list in Python?When would you choose to use an array over a list in Python?Apr 26, 2025 am 12:12 AM

Useanarray.arrayoveralistinPythonwhendealingwithhomogeneousdata,performance-criticalcode,orinterfacingwithCcode.1)HomogeneousData:Arrayssavememorywithtypedelements.2)Performance-CriticalCode:Arraysofferbetterperformancefornumericaloperations.3)Interf

Are all list operations supported by arrays, and vice versa? Why or why not?Are all list operations supported by arrays, and vice versa? Why or why not?Apr 26, 2025 am 12:05 AM

No,notalllistoperationsaresupportedbyarrays,andviceversa.1)Arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,whichimpactsperformance.2)Listsdonotguaranteeconstanttimecomplexityfordirectaccesslikearraysdo.

How do you access elements in a Python list?How do you access elements in a Python list?Apr 26, 2025 am 12:03 AM

ToaccesselementsinaPythonlist,useindexing,negativeindexing,slicing,oriteration.1)Indexingstartsat0.2)Negativeindexingaccessesfromtheend.3)Slicingextractsportions.4)Iterationusesforloopsorenumerate.AlwayschecklistlengthtoavoidIndexError.

How are arrays used in scientific computing with Python?How are arrays used in scientific computing with Python?Apr 25, 2025 am 12:28 AM

ArraysinPython,especiallyviaNumPy,arecrucialinscientificcomputingfortheirefficiencyandversatility.1)Theyareusedfornumericaloperations,dataanalysis,andmachinelearning.2)NumPy'simplementationinCensuresfasteroperationsthanPythonlists.3)Arraysenablequick

How do you handle different Python versions on the same system?How do you handle different Python versions on the same system?Apr 25, 2025 am 12:24 AM

You can manage different Python versions by using pyenv, venv and Anaconda. 1) Use pyenv to manage multiple Python versions: install pyenv, set global and local versions. 2) Use venv to create a virtual environment to isolate project dependencies. 3) Use Anaconda to manage Python versions in your data science project. 4) Keep the system Python for system-level tasks. Through these tools and strategies, you can effectively manage different versions of Python to ensure the smooth running of the project.

What are some advantages of using NumPy arrays over standard Python arrays?What are some advantages of using NumPy arrays over standard Python arrays?Apr 25, 2025 am 12:21 AM

NumPyarrayshaveseveraladvantagesoverstandardPythonarrays:1)TheyaremuchfasterduetoC-basedimplementation,2)Theyaremorememory-efficient,especiallywithlargedatasets,and3)Theyofferoptimized,vectorizedfunctionsformathematicalandstatisticaloperations,making

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use