gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.3KB

  1. #!C:\odoobuild\WinPy64\python-3.12.3.amd64\python.exe
  2. ##############################################################################
  3. #
  4. # vba_extract - A simple utility to extract a vbaProject.bin binary from an
  5. # Excel 2007+ xlsm file for insertion into an XlsxWriter file.
  6. #
  7. # SPDX-License-Identifier: BSD-2-Clause
  8. # Copyright 2013-2023, John McNamara, jmcnamara@cpan.org
  9. #
  10. import sys
  11. from zipfile import ZipFile
  12. from zipfile import BadZipFile
  13. def extract_file(xlsm_zip, filename):
  14. # Extract a single file from an Excel xlsm macro file.
  15. data = xlsm_zip.read("xl/" + filename)
  16. # Write the data to a local file.
  17. file = open(filename, "wb")
  18. file.write(data)
  19. file.close()
  20. # The VBA project file and project signature file we want to extract.
  21. vba_filename = "vbaProject.bin"
  22. vba_signature_filename = "vbaProjectSignature.bin"
  23. # Get the xlsm file name from the commandline.
  24. if len(sys.argv) > 1:
  25. xlsm_file = sys.argv[1]
  26. else:
  27. print(
  28. "\nUtility to extract a vbaProject.bin binary from an Excel 2007+ "
  29. "xlsm macro file for insertion into an XlsxWriter file.\n"
  30. "If the macros are digitally signed, extracts also a vbaProjectSignature.bin "
  31. "file.\n"
  32. "\n"
  33. "See: https://xlsxwriter.readthedocs.io/working_with_macros.html\n"
  34. "\n"
  35. "Usage: vba_extract file.xlsm\n"
  36. )
  37. exit()
  38. try:
  39. # Open the Excel xlsm file as a zip file.
  40. xlsm_zip = ZipFile(xlsm_file, "r")
  41. # Read the xl/vbaProject.bin file.
  42. extract_file(xlsm_zip, vba_filename)
  43. print("Extracted: %s" % vba_filename)
  44. if "xl/" + vba_signature_filename in xlsm_zip.namelist():
  45. extract_file(xlsm_zip, vba_signature_filename)
  46. print("Extracted: %s" % vba_signature_filename)
  47. except IOError as e:
  48. print("File error: %s" % str(e))
  49. exit()
  50. except KeyError as e:
  51. # Usually when there isn't a xl/vbaProject.bin member in the file.
  52. print("File error: %s" % str(e))
  53. print("File may not be an Excel xlsm macro file: '%s'" % xlsm_file)
  54. exit()
  55. except BadZipFile as e:
  56. # Usually if the file is an xls file and not an xlsm file.
  57. print("File error: %s: '%s'" % (str(e), xlsm_file))
  58. print("File may not be an Excel xlsm macro file.")
  59. exit()
  60. except Exception as e:
  61. # Catch any other exceptions.
  62. print("File error: %s" % str(e))
  63. exit()
上海开阖软件有限公司 沪ICP备12045867号-1