Advertisement
g96

Untitled

g96
Jan 19th, 2022
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. from calendar import month_name
  4. from pathlib import Path
  5.  
  6. filenames_array = [f'PB orders {m[:3]}.xlsb' for m in month_name[1:]]
  7. masterfile_sheetname_array = [f'{m[:3]}data' for m in month_name[1:]]
  8. my_path= Path("C:").resolve() / 'Load_attachments'
  9.  
  10.  
  11.  
  12. for filename in my_path.glob('*.xlsb'):
  13.     print(filename)
  14.     month = pd.read_excel(str(filename), sheet_name="Raw data ", engine="pyxlsb")
  15.     print(month, end='\n-------------------------------------------------------------------\n')
  16.     with pd.ExcelWriter(
  17.             my_path.parent / 'PB report.xlsx',
  18.             mode="a",
  19.             if_sheet_exists="replace",
  20.             engine="openpyxl",
  21.     ) as writer:
  22.         month.to_excel(
  23.             writer,
  24.             sheet_name=masterfile_sheetname_array[
  25.                 filenames_array.index(filename)
  26.             ],  # Since the lists share the same indeces, use the filename to retrieve the name of the sheet
  27.             engine="openpyxl",
  28.             index=False,
  29.         )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement