Advertisement
pastexale

Untitled

Dec 21st, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.68 KB | None | 0 0
  1. import pandas as pd
  2. import os
  3.  
  4. fnamebegin_array = [
  5.     "PB orders Jan",
  6.     "PB orders Feb",
  7.     "PB orders Mar",
  8.     "PB orders Apr",
  9.     "PB orders May",
  10.     "PB orders Jun",
  11.     "PB orders Jul",
  12.     "PB orders Aug",
  13.     "PB orders Sep",
  14.     "PB orders Oct",
  15.     "PB orders Nov",
  16.     "PB orders Dec",
  17. ]
  18.  
  19. sheetname_array = [
  20.     "JanData",
  21.     "FebData",
  22.     "MarData",
  23.     "AprData",
  24.     "MayData",
  25.     "JunData",
  26.     "JulData",
  27.     "AugData",
  28.     "SepData",
  29.     "OctData",
  30.     "NovData",
  31.     "DecData",
  32. ]
  33.  
  34. # Define the paths
  35. my_path = os.normpath(r"C:\Users\greencolor\Autoreport\Load_attachments")
  36. my_path1 = os.normpath(r"C:\Users\greencolor\Desktop\Autoreport")
  37.  
  38. # Retrieve the list of filenames (without extension) in my_path
  39. files_no_ext = [
  40.     os.path.splitext(f)[0] for f in os.listdir(my_path) if os.path.isfile(f)
  41. ]
  42.  
  43.  
  44. for filename in files_no_ext:  # Iterate over the list
  45.     if filename in fnamebegin_array:  # If the filename is in the list
  46.         # Create the 'month' DataFrame
  47.         month = pd.read_excel(
  48.             os.path.join(my_path, filename), sheet_name="Raw data ", engine="openpyxl"
  49.         )
  50.  
  51.         with pd.ExcelWriter(
  52.             os.path.join(my_path1, "Master.xlsx"),
  53.             mode="a",
  54.             if_sheet_exists="replace",
  55.             engine="openpyxl",
  56.         ) as writer:
  57.             month.to_excel(
  58.                 writer,
  59.                 sheet_name=sheetname_array[
  60.                     fnamebegin_array.index(filename)
  61.                 ],  # Since the lists share the same indeces, use the filename to retrieve the name of the sheet
  62.                 engine="openpyxl",
  63.                 index=False,
  64.             )
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement