Skip to content

Instantly share code, notes, and snippets.

@Kuanlin-Chen
Last active November 1, 2022 02:53
Show Gist options
  • Save Kuanlin-Chen/c189b61ca20658c3456e9ae07c63cddf to your computer and use it in GitHub Desktop.
Save Kuanlin-Chen/c189b61ca20658c3456e9ae07c63cddf to your computer and use it in GitHub Desktop.
Merge multiple Excel sheets
#!/usr/bin/env python
#
# Program:
# This program will merge multiple sheets in an excel file.
import sys
import xlwt
from xlrd import open_workbook
def main(orig_args):
license = {}
update_license_dict('old.xlsx', license)
print_license(license)
write_license_to_file('new.xlsx', license)
def update_license_dict(filename, license):
wb = open_workbook(filename)
for sheet in wb.sheets():
number_of_rows = sheet.nrows
for row in range(0,number_of_rows):
key = str(sheet.cell(row,0).value)
value = int(sheet.cell(row,1).value)
if key in license:
license[key] += value
else:
license.update({key:value})
def print_license(license):
count = 0
for key, value in license.iteritems() :
print key, value
count = count + 1
print(count)
def write_license_to_file(filename, license):
book = xlwt.Workbook(encoding='utf-8')
sheet1 = book.add_sheet('new sheet name')
sheet1.write(0, 0, 'License Name')
sheet1.write(0, 1, 'Scanner Count')
for count, (key, value) in enumerate(license.iteritems(), 1):
sheet1.write(count, 0, key)
sheet1.write(count, 1, value)
book.save(filename)
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment