Last active
May 31, 2021 12:11
-
-
Save rasel-rz/4e6bbc402720a497310a0aa0dfe50ede to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import openpyxl | |
from pathlib import Path | |
dir_containing_files = os.getcwd() + '\\data' | |
# print(dir_containing_files) | |
# print(type(dir_containing_files)) | |
dest_wb = openpyxl.Workbook() | |
collectables = [] | |
for root, dir, filenames in os.walk(dir_containing_files): | |
for file in filenames: | |
print(file) | |
xlsx_file = Path(os.getcwd() + '\\data\\' + file) | |
wb_obj = openpyxl.load_workbook(xlsx_file) | |
sheet = wb_obj.active | |
for row in sheet.values: | |
collectables.append(row) | |
sheet = dest_wb.active | |
for row in collectables: | |
sheet.append(row) | |
dest_wb.save("monthly-data.xlsx") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openpyxl | |
from pathlib import Path | |
import datetime | |
def modifyMyExcelSheet(filename, year, month, day): | |
xlsx_file = Path(filename) | |
wb_obj = openpyxl.load_workbook(xlsx_file) | |
sheet = wb_obj['RawData'] | |
# print(sheet) | |
collectables = [] | |
for row in sheet.values: | |
if 'Total Site Down' not in row: | |
collectables.append(list(row)) | |
# print(len(collectables)) | |
# print(type(collectables[7][7])) | |
may3rd1st = datetime.datetime(year, month, day) | |
may3rdlst = datetime.datetime(year, month, day, 23, 59, 59) | |
for row in collectables: | |
if type(row[9]) is type(may3rdlst): | |
if row[9] > may3rdlst: | |
row[9] = may3rdlst | |
if type(row[7]) is type(may3rd1st): | |
if row[7] < may3rd1st: | |
row[7] = may3rd1st | |
myExport = openpyxl.Workbook() | |
mySheet = myExport.active | |
for row in collectables: | |
myRow = tuple(row) | |
mySheet.append(myRow) | |
myExport.save('modified - ' + filename) | |
print('File saved as - ' + '"modified - ' + filename + '"') | |
modifyMyExcelSheet('3G_SiteAvailabilityReport_SOC_Day-2_05-05-21.xlsx', 2021, 5, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment