使用python限制excel文件中工作表的数量

2024-09-26 17:59:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要使用python将excel文件中的工作表数量限制为特定数量。一旦工作表中的工作表数达到特定数目,用户就不能添加新的工作表。

我找不到任何使用xlsxwriteropenpyxlxlrd的解决方案

有没有其他软件包可供选择?


Tags: 文件用户数量解决方案excel数目openpyxlxlrd
1条回答
网友
1楼 · 发布于 2024-09-26 17:59:37

Excel没有内置这样的功能。You can only disallow creating new sheets by protecting the workbook or with a VBA handler that reverses the operation immediately。在

保护工作簿also disables other worksheet operations,如移动、重命名和隐藏/取消隐藏,这可能是不可取的。在

哦,VBA处理程序可以比链接上的处理程序更智能:

Private Sub Workbook_NewSheet(ByVal Sh As Object)
    If ThisWorkbook.Worksheets.Count > <Maximum> Then
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            Sh.Delete
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
    End If
End Sub

当然,如果使用不运行VBA的第三方软件包编辑文件,或者禁用Excel中的宏,这将不会有任何效果。在

若要包含宏,工作簿必须另存为.xlsm,否则Excel在打开时会出错。在

请参见Working with VBA Macros — XlsxWriter Documentation关于Python实现。^{} cannot work with macros, only preserve them at most,和^{} looks like being designed to only read rather than edit。或者,pywin32可以使用Excel自己的COM接口。在

相关问题 更多 >

    热门问题