excel文档怎么修改创建时间(图文)
修改Excel文档的创建时间有多种方法,包括修改文件系统时间戳和文档内部属性。以下是详细的实现方法:
Python 脚本示例:
修改文件系统时间戳
Windows 系统
方法一:使用 PowerShell(推荐)
powershell
# 修改单个 Excel 文件的创建时间 $excelFile = Get-Item "C:\path\to\your\document.xlsx" $excelFile.CreationTime = "2024-01-01 10:00:00" $excelFile.LastWriteTime = "2024-01-01 10:00:00" # 批量修改多个 Excel 文件 Get-ChildItem "C:\path\to\folder" -Include *.xlsx,*.xls -Recurse | ForEach-Object { $_.CreationTime = "2024-01-01 10:00:00" $_.LastWriteTime = "2024-01-01 10:00:00" Write-Host "已修改: $($_.Name)" }方法二:使用 CMD 命令
cmd
# 需要安装 Windows 版本的 touch 工具 touch -t 202401011000 document.xlsxLinux/macOS 系统
bash
# 使用 touch 命令修改时间戳 touch -t 202401011000 document.xlsx # 批量修改 find /path/to/folder -name "*.xlsx" -o -name "*.xls" | xargs touch修改 Excel 文档内部属性
方法一:使用 VBA 宏
- 打开 Excel 文档
- 按
Alt + F11打开 VBA 编辑器 - 插入新模块并输入以下代码:
vba
Sub ModifyDocumentCreationTime() ' 修改当前工作簿的创建时间属性 On Error Resume Next ThisWorkbook.BuiltinDocumentProperties("Creation Date").Value = "2024-01-01 10:00:00" ThisWorkbook.BuiltinDocumentProperties("Last Save Time").Value = "2024-01-01 10:00:00" ThisWorkbook.BuiltinDocumentProperties("Author").Value = "Your Name" MsgBox "文档创建时间已更新" ' 保存工作簿以应用更改 ThisWorkbook.Save End Sub- 运行宏即可修改文档属性
方法二:使用 Python 脚本(推荐)
首先安装必要的库:bash
pip install openpyxl xlsxwriterpython
from openpyxl import load_workbook import os from datetime import datetime def modify_excel_document_properties(file_path, creation_date=None, author=None): """ 修改 Excel 文档的内部属性 :param file_path: Excel 文件路径 :param creation_date: 新的创建日期 :param author: 作者信息 """ if creation_date is None: creation_date = datetime.now() try: # 加载工作簿 wb = load_workbook(file_path) # 修改文档属性 props = wb.properties props.creator = author if author else "Modified User" props.created = creation_date props.modified = creation_date # 保存文件 wb.save(file_path) # 同时修改文件系统时间戳 timestamp = creation_date.timestamp() os.utime(file_path, (timestamp, timestamp)) print(f"成功修改文件 {file_path} 的创建时间") return True except Exception as e: print(f"修改失败: {e}") return False # 使用示例 new_date = datetime(2024, 1, 1, 10, 0, 0) modify_excel_document_properties("document.xlsx", new_date, "张三")方法三:使用 pandas + openpyxl
python
import pandas as pd from openpyxl import load_workbook import os from datetime import datetime def modify_excel_with_preserved_data(file_path, new_creation_date=None): """ 在保持数据不变的情况下修改 Excel 文件创建时间 """ if new_creation_date is None: new_creation_date = datetime.now() try: # 读取所有工作表 excel_file = pd.ExcelFile(file_path) sheet_names = excel_file.sheet_names # 使用 ExcelWriter 重新写入文件 with pd.ExcelWriter(file_path, engine='openpyxl') as writer: writer.book = load_workbook(file_path) writer.sheets = {ws.title: ws for ws in writer.book.worksheets} # 重新写入每个工作表的数据 for sheet_name in sheet_names: df = pd.read_excel(file_path, sheet_name=sheet_name) df.to_excel(writer, sheet_name=sheet_name, index=False) # 修改文档属性 writer.book.properties.creator = "Modified User" writer.book.properties.created = new_creation_date writer.book.properties.modified = new_creation_date # 修改文件系统时间戳 timestamp = new_creation_date.timestamp() os.utime(file_path, (timestamp, timestamp)) print(f"成功更新 {file_path} 的创建时间") except Exception as e: print(f"操作失败: {e}") # 使用示例 # new_date = datetime(2024, 1, 1, 10, 0, 0) # modify_excel_with_preserved_data("document.xlsx", new_date)批量处理脚本
Python 批量修改脚本
python
import os from pathlib import Path from openpyxl import load_workbook from datetime import datetime def batch_modify_excel_creation_time(folder_path, new_date=None, recursive=False): """ 批量修改文件夹中所有 Excel 文件的创建时间 :param folder_path: 文件夹路径 :param new_date: 新的创建日期 :param recursive: 是否递归处理子文件夹 """ if new_date is None: new_date = datetime.now() # 支持的 Excel 文件扩展名 excel_extensions = {'.xlsx', '.xls', '.xlsm', '.xlsb'} # 确定搜索模式 if recursive: files = Path(folder_path).rglob('*') else: files = Path(folder_path).glob('*') # 处理每个 Excel 文件 for file_path in files: if file_path.suffix.lower() in excel_extensions: try: # 修改文档属性 wb = load_workbook(file_path) props = wb.properties props.creator = "Batch Modified" props.created = new_date props.modified = new_date wb.save(file_path) # 修改文件系统时间戳 timestamp = new_date.timestamp() os.utime(file_path, (timestamp, timestamp)) print(f"✓ 已修改: {file_path}") except Exception as e: print(f"✗ 修改失败 {file_path}: {e}") # 使用示例 # batch_modify_excel_creation_time("C:/ExcelFiles", datetime(2024, 1, 1, 10, 0, 0), recursive=True)使用第三方工具
ExifTool(命令行工具)
bash
# 下载并安装 ExifTool # 修改单个文件 exiftool -createdate="2024:01:01 10:00:00" document.xlsx exiftool -modifydate="2024:01:01 10:00:00" document.xlsx # 批量修改 exiftool -createdate="2024:01:01 10:00:00" *.xlsx验证修改结果
查看文件属性的方法:
Windows PowerShell
powershell
# 查看详细时间信息 Get-Item "document.xlsx" | Select-Object Name, CreationTime, LastWriteTime, LastAccessTime # 查看文档属性 $shell = New-Object -ComObject Shell.Application $folder = $shell.Namespace("C:\path\to\folder") $file = $folder.ParseName("document.xlsx") $folder.GetDetailsOf($file, 4) # 创建日期Linux/macOS
bash
# 查看文件时间戳 stat document.xlsx ls -l document.xlsx # 使用 exiftool 查看文档属性 exiftool document.xlsx | grep -i date注意事项
- 备份重要文件:修改前务必备份原始文件
- 权限要求:确保有足够的权限修改文件
- 格式兼容性:不同版本的 Excel 对属性支持可能不同
- 宏启用:包含宏的文件 (.xlsm) 需要注意安全性
- 云同步影响:如果文件在云同步目录中,修改可能触发重新同步
- 数字签名:修改时间可能使数字签名失效

更新时间:2025-12-17 09:46:55
