简体   繁体   English

如何使用Python xlwings在excel工作簿中检测受保护的工作表?

[英]How to detect protected worksheet in excel workbook using Python xlwings?

I have a script running to update excel spreadsheets from our timesheet MongoDB database. 我正在运行一个脚本,用于从时间表MongoDB数据库中更新excel电子表格。 Essentially, people are inputting their timesheets in my webapp and I update their excel spreadsheets via Python script. 本质上,人们是在我的Web应用程序中输入他们的时间表,然后我通过Python脚本更新他们的excel电子表格。

However, when billing is done for a month, we protect that sheet. 但是,当结算一个月后,我们会保护该工作表。 Sometimes, there's a cross over of the update being in the two week period in a protected sheet. 有时,在受保护的工作表中的两周时间内会有更新的交叉。

My issue is my current script wipes the data and rewrites it essentially in that two week time period. 我的问题是我当前的脚本擦除了数据并基本上在那两周的时间段内对其进行了重写。 But on the protected sheet, it will keep attempting to write to it even though it protected and will not continue. 但是在受保护的工作表上,即使受保护,它仍会继续尝试对其进行写操作,并且不会继续。 It doesn't throw an error, and keeps trying to execute that line of code. 它不会引发错误,并且会继续尝试执行该行代码。

I tried implementing a timeout because the line doesn't throw an error and python can't catch it. 我尝试实现超时,因为该行不会引发错误,而python无法捕获该错误。 However, python doesn't check the while statement constantly, so it will see the current time is initially less than the timeout, but never rechecks it and can't because it gets stuck on the line of code. 但是,python不会一直检查while语句,因此它将看到当前时间最初小于超时时间,但是从不重新检查它,并且因为它卡在了代码行中而无法进行检查。

for j,user in enumerate(users):
    if user not in email_timesheet_dict:
        continue
    wb = xw.Book(email_timesheet_dict[user])
    app = xw.apps.active
    for sheet in sheets:
        time.sleep(1)                                 //What I've tried
        timeout = 30                                  //
        timeout_start = time.time()                   //
        print(time.time())                            //
        print(timeout_start)                          //
        print(timeout + timeout_start)                //
        while time.time() < timeout_start + timeout:  //What I've tried
            sht =wb.sheets[sheet]
            ...
            ...

This is the line of code it gets stuck on: 这是它停留在的代码行:

sht.range(f"{col}{row}").value = None

I'm wondering if there's a way to check if the sheet i'm editing is protected, and if so it will skip it and go to the next sheet. 我想知道是否有一种方法可以检查我正在编辑的工作表是否受保护,如果可以,它将跳过它并转到下一个工作表。 It's an older excel file (.xls), so I need it to be compatible. 这是一个较旧的excel文件(.xls),因此我需要使其兼容。

python doesn't check the while statement python不检查while语句

Because the application is hanging on something within that statement. 因为应用程序挂在该语句中的某个内容上。 Typically there is a dialog box that informing the user of the protection: 通常,有一个对话框通知用户保护:

在此处输入图片说明

The Excel Application is unresponsive for as long as that dialog box is displayed. 只要显示该对话框,Excel应用程序就不会响应。 Your best bet is probably to ignore protected sheets. 最好的选择是忽略受保护的床单。 Untested, but explained in comments: 未经测试,但在注释中说明:

for sheet in sheets:
    sht = wb.sheets[sheet]
    alerts = wb.api.Application.DisplayAlerts  # get the current value of this property
    wb.api.Application.DisplayAlerts = False   # suppress the warning dialog which is freezing Excel Application
    try:
        sht.range(f'{col}{row}').value = None
    except:
        # if the range is not writeable, an error should be raised here.
        print(f'{sheet} is protected and can''t be written to...')
    finally:
        # return the application.DisplayAlerts to its previous value
        wb.api.Application.DisplayAlerts = alerts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 xlwings 刷新工作簿中的单个工作表(excel) - Refresh single worksheet (excel) in workbook using xlwings 如何使用xlwings在python for循环中打开和关闭excel工作簿 - How to open and close an excel workbook in a python for loop using xlwings 如何使用 Xlwings 和 Python 合并同一个 Excel 工作簿中的所有工作表 - How to Merge all worksheets within the same excel workbook using Xlwings & Python 如何使用 XLWINGS 将列过滤器添加到 Excel 工作表? - How to add column filters to Excel worksheet using XLWINGS? 有什么方法可以检测 excel 工作簿是否在 Python 或机器人框架中受密码保护? - Is there any way to detect if an excel workbook is password protected in Python or Robot framework? 如何在 Python 中使用 xlwings 获取/设置工作表选项卡的颜色? - How to get/set the colour of the worksheet tab using xlwings in Python? 使用 xlwings 写入现有 Excel 工作簿 - Writing to an existing Excel Workbook using xlwings 使用python使用excel工作表(具有所需条件)创建excel工作簿 - Creating excel workbook using excel worksheet (with required condition) using python 将受保护的Excel工作簿复制到另一个工作簿python中 - Copy Protected excel workbook into another workbook python 使用 xlwings 和 python 复制工作表 - Copying a worksheet with xlwings and python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM