[text] B

Viewer

  1. import requests
  2. import os
  3.  
  4. # Import the Databricks API for DBFS
  5. from dbutils import DBUtils
  6.  
  7. # URL for the main action
  8. url = 'https://toolkit.tesco.com/partner/reports/'
  9.  
  10. # Define options for each category/subcategory
  11. Country = ['UK', 'ROI']
  12. Report_type = ['Sales and stock', 'Range conformance', 'Promotional funding', 'Auto sales out retro deals', 'Fulfilment fee', 'Performance summary', 'Cost price amendments', 'Purchase order amendments']
  13. Product_subgroup = ['All product subgroups']
  14. Products = ['All products']
  15. View = ['TPNB – Total sales', 'TPNB – Sales x store', 'TPNB – Sales x store format', 'TPNB - Sales x DC','Product subgroup - Total sales','Stores - Total sales','Store Format - Total sales']
  16. Time_period = ['Last full week (Wk 08)']
  17.  
  18. # Function to prompt user to select an option
  19. def select_option(options, prompt):
  20.     print(prompt)
  21.     for i, option in enumerate(options):
  22.         print(f"{i + 1}. {option}")
  23.     while True:
  24.         choice = input("Enter your choice (1-{}): ".format(len(options)))
  25.         if choice.isdigit() and 1 <= int(choice) <= len(options):
  26.             return options[int(choice) - 1]
  27.         else:
  28.             print("Invalid choice. Please enter a number between 1 and {}.".format(len(options)))
  29.  
  30. # Prompt user to select options
  31. country = select_option(Country, "Select country:")
  32. report_type = select_option(Report_type, "Select report type:")
  33. product_subgroup = select_option(Product_subgroup, "Select product subgroup:")
  34. product = select_option(Products, "Select product:")
  35. view = select_option(View, "Select view:")
  36. time_period = select_option(Time_period, "Select time period:")
  37.  
  38. # Send request to select options
  39. options_data = {
  40.     'Country': country,
  41.     'Report type': report_type,
  42.     'Product subgroup': product_subgroup,
  43.     'Product': product,
  44.     'View': view,
  45.     'Time period': time_period
  46. }
  47.  
  48. try:
  49.     response = requests.get(url)#, params=options_data)
  50.     response.raise_for_status()  # Raise exception for 4xx or 5xx errors
  51.     # Process the response as needed
  52.     print("Options selected:", options_data)
  53.     
  54.     # Save the response content to a file with .xlsx extension
  55.     file_name = f"{country}_{report_type}_{product}_{view}_{time_period}.xlsx"
  56.     file_path = os.path.join("/dbfs", "path", "to", "save", file_name)
  57.     with open(file_path, "wb") as f:
  58.         f.write(response.content)
  59.     
  60.     # Print the file path where the report is saved
  61.     print("Report saved to DBFS:", file_path)
  62. except requests.exceptions.HTTPError as err:
  63.     print("An error occurred:", err)

Editor

You can edit this paste and save as new:


File Description
  • B
  • Paste Code
  • 26 Apr-2024
  • 2.55 Kb
You can Share it: