How to modify the metadata of Excel files in Python

王林
Release: 2023-05-18 17:46:41
forward
1933 people have browsed it

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!

Related labels:
source:yisu.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template