【发布时间】:2018-11-28 00:47:01
【问题描述】:
需要帮助遍历一个充满 xlsx 和 xlsm 文档的目录并更改 set_properties。 我有大约 10k 个 excel 文件(xlsx 和 xlsm,没有 xls 文件)。我正在尝试遍历它们并更改 set_properties。 我编写的代码对所有文档进行了正确的更改,但是当我尝试打开文档时,所有内容(所有工作表和每个工作表中的所有内容)都消失了。 感谢您的帮助!
import os
from openpyxl import Workbook
import xlsxwriter
import datetime
os.chdir('C:\\Users\\bayli\\Desktop\\unscrubbed')
mydate = datetime.datetime.now()
files_not_scrubbed = 0
for filename in os.listdir():
if filename.endswith(".xlsx") or filename.endswith(".XLSX") or
filename.endswith(".xlsm") or filename.endswith(".XLSM"):
currentBook = xlsxwriter.Workbook(filename)
currentBook.set_properties({
'title': 'TEST',
'subject': 'TEST',
'author': 'TEST',
'manager': 'TEST',
'company': 'TEST',
'category': 'TEST',
'keywords': 'TEST',
'comments': 'TEST',
'status': 'TEST',
'create': mydate,
})
currentBook.close()
else:
files_not_scrubbed = files_not_scrubbed + 1
continue
print("There were " + str(files_not_scrubbed) + " files not scrubbed.")
【问题讨论】: