How to modify the metadata of Excel files through Python - create your own "My Excel"

 

Table of contents

 

Application scenario:

The effect is as follows:

 Test Data:

 source code:

Source code description:


Application scenario:

        This code can be used to modify the metadata of an Excel file, such as author, subject, description, etc. By using Python and the Openpyxl module, as well as the wxPython library, we can create a GUI interface to enter metadata, and then combine these metadata with Excel file together.

Here are a few possible application scenarios:

  1. Data management: When you need to classify and manage a large number of Excel files, metadata can help you identify and find these files more quickly.

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

  3. Data analysis: When you need to perform data analysis among multiple Excel files, metadata allows you to identify and distinguish these files more quickly.

  4. Data reporting: When you need to cite 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 that needs to modify the metadata of an Excel file, from data management to data analysis to data reporting, all can be achieved through this code.

The effect is as follows:

 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:

        It uses the wxPython module to create a GUI interface that allows the user to input the properties of the Excel file to be modified. User can enter Author, Creation Time, Modification Time, Title, Subject and Description and click "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 that contains text boxes for entering the properties of the Excel file and a "Save" button. When the "Save" button is clicked, it will obtain the Excel file that the user wants to modify, and save the input attribute value to the attribute of the Excel file. Then, it displays a message box to inform the user that the save was successful.

        Note that this procedure assumes that the user already knows the path to the Excel file to be modified. If you need to let the user browse the file system to select an Excel file, you can use the file dialog of the wxPython module.

       

Guess you like

Origin blog.csdn.net/winniezhang/article/details/130241190