好友
阅读权限10
听众
最后登录1970-1-1
|
😊
**批量修改 Word、Excel、PPT 文档属性详细信息的 Python 脚本**
===========================================================
**安装库**
```bash
pip install python-docx openpyxl python-pptx
```
**脚本**
```python
import os
import docx
import openpyxl
from pptx import Presentation
def modify_word_properties(file_path, new_properties):
"""
修改 Word 文档属性
"""
doc = docx.Document(file_path)
for key, value in new_properties.items():
if key == "title":
doc.core_properties.title = value
elif key == "author":
doc.core_properties.author = value
elif key == "created":
doc.core_properties.created = value
elif key == "modified":
doc.core_properties.modified = value
doc.save(file_path)
def modify_excel_properties(file_path, new_properties):
"""
修改 Excel 文档属性
"""
wb = openpyxl.load_workbook(file_path)
for key, value in new_properties.items():
if key == "title":
wb.properties.title = value
elif key == "author":
wb.properties.author = value
elif key == "created":
wb.properties.created = value
elif key == "modified":
wb.properties.modified = value
wb.save(file_path)
def modify_ppt_properties(file_path, new_properties):
"""
修改 PPT 文档属性
"""
prs = Presentation(file_path)
for key, value in new_properties.items():
if key == "title":
prs.core_properties.title = value
elif key == "author":
prs.core_properties.author = value
elif key == "created":
prs.core_properties.created = value
elif key == "modified":
prs.core_properties.modified = value
prs.save(file_path)
def main():
# 文件夹路径
folder_path = "path/to/folder"
# 新的属性值
new_properties = {
"title": "新标题",
"author": "新作者",
"created": "2024-08-16",
"modified": "2024-08-16"
}
# 遍历文件夹中的文件
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
# 判断文件类型
if file_name.endswith(".docx"):
modify_word_properties(file_path, new_properties)
elif file_name.endswith(".xlsx"):
modify_excel_properties(file_path, new_properties)
elif file_name.endswith(".pptx"):
modify_ppt_properties(file_path, new_properties)
if __name__ == "__main__":
main()
```
**使用方法**
1. 将脚本文件保存为 `modify_properties.py`。
2. 修改 `folder_path` 变量为需要批量修改的文件夹路径。
3. 修改 `new_properties` 变量为新的属性值。
4. 运行脚本文件 `python modify_properties.py`。
脚本将批量修改文件夹中的 Word、Excel、PPT 文件的属性详细信息。 |
|