I'm trying to merge three different workbooks (one sheet in each one) into a single one, transforming each in a new sheet. Code works like a charm when there is 'a', 'b, and 'c', but sometimes there won't be one or two of the three files, and I still need to create a workbook with just 'A' and 'C', 'B' and 'C', just 'A', etc.
import pandas as pd
import xlsxwriter
car_list = pd.read_csv(f'C:/Users/Downloads/cars.csv)
car = car_list['Car Name']
for i in range(len(car)):
cars = car[i]
if pd.notna(cars):
a = f'C:/Users/Downloads/A/{car[i]}.xlsx'
b = f'C:/Users/Downloads/B/{car[i]}.xlsx'
c = f'C:/Users/Downloads/C/{car[i]}.xlsx'
writer = pd.ExcelWriter(f'C:/Users/Downloads/Output/{car[i]}.xlsx',engine='xlsxwriter')
reada = pd.read_excel(a)
readb = pd.read_excel(b)
readc = pd.read_excel(c)
reada.to_excel(writer, sheet_name='A')
readb.to_excel(writer, sheet_name='B')
readc.to_excel(writer, sheet_name='C')
writer.save()
I already tried the addition below, but, for example: if there isn't 'B' for car 2, it just uses 'B' of car 1. How can I make it work?
try:
reada = pd.read_excel(a)
readb = pd.read_excel(b)
readc = pd.read_excel(c)
except FileNotFoundError:
pass
Edit: I don't know if it is the best way, but the following code worked just fine for that:
if os.path.exists(a) is True:
pd.read_excel(a).to_excel(writer, sheet_name='A')
else:
print(car[i]+' without A')
if os.path.exists(b) is True:
pd.read_excel(b).to_excel(writer, sheet_name='B')
else:
print(car[i] + ' without B')
if os.path.exists(c) is True:
pd.read_excel(c).to_excel(writer, sheet_name='C')
else:
print(car[i] + ' without C')
writer.save()