【问题标题】:python looping through excel documents losing excel document data after changing set propertiespython循环遍历excel文档,更改设置属性后丢失excel文档数据
【发布时间】: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.")

【问题讨论】:

    标签: python excel


    【解决方案1】:

    xlsxwriter 使用新属性创建一个新工作簿。尝试使用openpyxl,如answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多