facturex-pdfextractxml.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # © 2017 Alexis de Lattre <alexis.delattre@akretion.com>
  4. from optparse import OptionParser
  5. import sys
  6. from facturx import get_facturx_xml_from_pdf
  7. from facturx.facturx import logger
  8. import logging
  9. from os.path import isfile, isdir
  10. __author__ = "Alexis de Lattre <alexis.delattre@akretion.com>"
  11. __date__ = "August 2017"
  12. __version__ = "0.1"
  13. options = [
  14. {'names': ('-l', '--log-level'), 'dest': 'log_level',
  15. 'action': 'store', 'default': 'info',
  16. 'help': "Set log level. Possible values: debug, info, warn, error. "
  17. "Default value: info."},
  18. {'names': ('-d', '--disable-xsd-check'), 'dest': 'disable_xsd_check',
  19. 'action': 'store_true', 'default': False,
  20. 'help': "De-activate XML Schema Definition check on Factur-X XML file "
  21. "(the check is enabled by default)"},
  22. ]
  23. def main(options, arguments):
  24. if options.log_level:
  25. log_level = options.log_level.lower()
  26. log_map = {
  27. 'debug': logging.DEBUG,
  28. 'info': logging.INFO,
  29. 'warn': logging.WARN,
  30. 'error': logging.ERROR,
  31. }
  32. if log_level in log_map:
  33. logger.setLevel(log_map[log_level])
  34. else:
  35. logger.error(
  36. 'Wrong value for log level (%s). Possible values: %s',
  37. log_level, ', '.join(log_map.keys()))
  38. sys.exit(1)
  39. if len(arguments) != 2:
  40. logger.error(
  41. 'This command requires 2 arguments (%d used). '
  42. 'Use --help to get the details.', len(arguments))
  43. sys.exit(1)
  44. pdf_filename = arguments[0]
  45. out_xml_filename = arguments[1]
  46. if not isfile(pdf_filename):
  47. logger.error('Argument %s is not a filename', pdf_filename)
  48. sys.exit(1)
  49. if isdir(out_xml_filename):
  50. logger.error(
  51. '2nd argument %s is a directory name (should be a the '
  52. 'output XML filename)', out_xml_filename)
  53. sys.exit(1)
  54. pdf_file = open(pdf_filename)
  55. check_xsd = True
  56. if options.disable_xsd_check:
  57. check_xsd = False
  58. # The important line of code is below !
  59. try:
  60. (xml_filename, xml_string) = get_facturx_xml_from_pdf(
  61. pdf_file, check_xsd=check_xsd)
  62. except Exception as e:
  63. logger.error(e)
  64. sys.exit(1)
  65. if xml_filename and xml_string:
  66. if isfile(out_xml_filename):
  67. logger.warn(
  68. 'File %s already exists. Overwriting it!', out_xml_filename)
  69. xml_file = open(out_xml_filename, 'w')
  70. xml_file.write(xml_string)
  71. xml_file.close()
  72. logger.info('File %s generated', out_xml_filename)
  73. else:
  74. logger.warn('File %s has not been created', out_xml_filename)
  75. sys.exit(1)
  76. if __name__ == '__main__':
  77. usage = "Usage: facturx-pdfextractxml <invoice.pdf> <factur-x_xml.xml>"
  78. epilog = "Author: %s\n\nVersion: %s" % (__author__, __version__)
  79. description = "This extracts the XML file from a Factur-X invoice."
  80. parser = OptionParser(usage=usage, epilog=epilog, description=description)
  81. for option in options:
  82. param = option['names']
  83. del option['names']
  84. parser.add_option(*param, **option)
  85. options, arguments = parser.parse_args()
  86. sys.argv[:] = arguments
  87. main(options, arguments)