如何在Python中检测xlsx文件是否受密码保护?

huangapple go评论93阅读模式
英文:

How to detect if the xlsx file is password protected or not in python?

问题

我需要检测Excel文件是否受密码保护。如果受密码保护,请要求用户输入密码。我该如何在Python中实现这一功能?我在互联网上查找了一些信息,目前还没有找到有用的答案。

英文:

I have a required where I need to detect if the excel file is password protected or not. If password protected ask user for the password.
How do I do this in python?
I looked a bit on the internet, none of the answers were useful so far

答案1

得分: 1

以下是您要翻译的内容:

  1. You could do something along these lines I guess.<br>
  2. If the file is encrypted it will pass and the password can be requested.<br>
  3. If it's not, it will error and the file can be loaded normally.<br>
  4. import openpyxl
  5. import io
  6. import msoffcrypto
  7. from msoffcrypto.exceptions import FileFormatError # pip install msoffcrypto-tool
  8. filename = 'foo.xlsx'
  9. with open(filename, 'rb') as file:
  10. try:
  11. workbook = io.BytesIO()
  12. office_file = msoffcrypto.OfficeFile(file)
  13. print("File is password protected")
  14. passwd = input("Please enter file password")
  15. office_file.load_key(password=passwd)
  16. office_file.decrypt(workbook)
  17. except FileFormatError as e:
  18. print(e)
  19. print("File is not password protected")
  20. workbook=filename
  21. wb = openpyxl.load_workbook(workbook)
  22. ws = wb['Sheet1']
  23. print(ws['A1'].value)
英文:

You could do something along these lines I guess.<br>
If the file is encrypted it will pass and the password can be requested.<br>
If it's not, it will error and the file can be loaded normally.<br>

  1. import openpyxl
  2. import io
  3. import msoffcrypto
  4. from msoffcrypto.exceptions import FileFormatError # pip install msoffcrypto-tool
  5. filename = &#39;foo.xlsx&#39;
  6. with open(filename, &#39;rb&#39;) as file:
  7. try:
  8. workbook = io.BytesIO()
  9. office_file = msoffcrypto.OfficeFile(file)
  10. print(&quot;File is password protected&quot;)
  11. passwd = input(&quot;Please enter file password&quot;)
  12. office_file.load_key(password=passwd)
  13. office_file.decrypt(workbook)
  14. except FileFormatError as e:
  15. print(e)
  16. print(&quot;File is not password protected&quot;)
  17. workbook=filename
  18. wb = openpyxl.load_workbook(workbook)
  19. ws = wb[&#39;Sheet1&#39;]
  20. print(ws[&#39;A1&#39;].value)

答案2

得分: 0

我找到了解决方案,在这个链接中 - https://github.com/nolze/msoffcrypto-tool/blob/03d3298f7b8e27a26280bab1702bad36cce2e398/msoffcrypto/__main__.py#L23

  1. import msoffcrypto
  2. file_c = msoffcrypto.OfficeFile(content)
  3. is_pwd_protected = file_c.is_encrypted() # 如果文件受密码保护,is_encrypted将返回True。
英文:

after digging into the library I found the solution - https://github.com/nolze/msoffcrypto-tool/blob/03d3298f7b8e27a26280bab1702bad36cce2e398/msoffcrypto/__main__.py#L23

  1. import msoffcrypto
  2. file_c = msoffcrypto.OfficeFile(content)
  3. is_pwd_protected = file_c.is_encrypted() #is_encrypted returns True if the file is password protected.

huangapple
  • 本文由 发表于 2023年5月13日 19:07:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定