Python to merge excel files based on names as sheets into one workbook

Viewed 56

I have a python program that already does the following:

  1. Takes existing excel spreadsheet and splits it into multiple separate files based on column value (let's customer ID, so there is a separate excel file for each customer_ID).
  2. Formats and names these separate excel files in specific way. File name format "customer_customer_ID_date", e.g. if customer_id is 023 then the file name is "customer_023_2022_08_16"
  3. Compresses these saparate excel files into one zip file.
  4. Emails the zipped file to specific people.

Now, I need to update this program to make it do the following (new feature in bold) :

  1. Takes existing excel spreadsheet and splits it into multiple separate files based on column value.
  2. Formats and names these separate excel files in specific way.

3. Add each of these customer_ID files as separate sheet of another excel workbook from a separate folder based on customer_ID in a target file name. For example, "customer_023_2022_08_16" file will become a new sheet named "Customer" in existing target workbook named "report_023_2022_08_16". So it should compare the 3 digit customer id in file names to add correct customer file a new sheet to corresponding customer workbook. And keep all formatting including frozen panes in both source and target files.

  1. Compresses these new separate excel files into one zip file.
  2. Emails the zipped file to specific people.

Below is my code. Can anybody help me to add the new functionality described in item #3 above?

import pandas as pd
import numpy as np
import sys
import glob
import zipfile
from io import StringIO
from io import BytesIO
import time
import win32com.client as win32
import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import PatternFill, Font
import os
import datetime
import glob
import base64
import logging
#imports for email
import smtplib,ssl
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import formatdate
from email import encoders

df = pd.read_excel('ALL_CUSTOMERS.xlsx')

# list all the sites (unique values)
customerIDs = list(df['Customer'].unique())

def format_col_width(ws):
    ws.set_column('A:B', 13)
    ws.set_column('C:C', 22)
    ws.set_column('D:D', 5)
    ws.set_column('E:F', 50)
    ws.set_column('G:H', 22)
    ws.set_column('I:I', 50)
    ws.set_column('J:N', 18)
    ws.set_column('M:M', 20)

from datetime import datetime

curr_date = datetime.strftime(datetime.now(), '%Y_%m_%d')

for customerID in customerIDs:
 filtered_df = df[df['Customer'] == customerID]
 filtered_df.drop(columns= ['Customer'], axis=1, inplace=True)

 filtered_df.to_excel('customer_' + customerID.astype(str).rjust(3, '0') + '_' + curr_date + '.xlsx', index=False, freeze_panes=(1, 2))
 writer = pd.ExcelWriter('customer_' + customerID.astype(str).rjust(3, '0') + '_' + curr_date + '.xlsx', engine='xlsxwriter')
 #filtered_df.drop(columns= [''], axis=1, inplace=True)
 filtered_df.to_excel(writer, sheet_name='Sheet1', index=False, freeze_panes=(1, 2))
 workbook  = writer.book
 worksheet = writer.sheets['Sheet1']
 #DelCols(worksheet)
 #worksheet = writer.getWorksheets().get(0)
 #worksheet.getCells().deleteColumns(1,1,True)
 format_col_width(worksheet)
 format = workbook.add_format({'text_wrap': True})
 worksheet.set_column('E:F', 50, format)
 worksheet.set_column('I:I', 50, format)
 writer.save()

from zipfile import ZipFile
folder = r'C:\Users'


from os import listdir
#directory = raw_input('C:\Users\')
files_dir =  listdir(folder)
newlist = []
for names in files_dir:
    if names.startswith("customer_"):
        newlist.append(names)

with ZipFile('customer_' + curr_date + '.zip','w') as zip:
        # writing each file one by one
        for file in newlist:
            zip.write(file)
cumulativeAddressString = 'customer_' + curr_date + '.zip'
###Send Email
#initialize lists
#recipientList = ''
ccList = ''
recipientList = 
#ccList = ['']
#recipientList = list(df_recipient_info['Recipients'].dropna().unique())
#ccList = list(df_recipient_info['CC'].dropna().unique())
#recipientList = recipientList + ccList
#send
def send_mail(send_from,send_to,subject,text,files,server,port,username='',isTls=True):
    msg = MIMEMultipart()
    msg['From'] = send_from
    for item in send_to:
        msg['To'] = item
    msg['Date'] = formatdate(localtime = True)
    msg['Subject'] = subject
    msg.attach(MIMEText(text))
    
    
    for file in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload(open(file, "rb").read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="' + file.rsplit('\\', 1)[-1] + '"')
        msg.attach(part)

    smtp = smtplib.SMTP(server, port)
    if isTls:
        smtp.starttls()
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.quit()

send_mail(send_from = 
send_to = 
subject = 
text = 
files = [cumulativeAddressString],
server = 
port = 
isTls=False)
0 Answers
Related