Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, csv
- from openpyxl import load_workbook, Workbook
- def main():
- #Ask for path to excel file
- excelpath = input("Path to excel file: ")
- #Ask for keyword
- txtorkey = input("Type textfile for .txt or keyword for unique keyword: ")
- if txtorkey == "textfile":
- txtfile = input("Path to textfile: ")
- btext = True
- elif txtorkey == "keyword":
- key = str.lower(input("keyword (single): "))
- btext = False
- #Ask for in which column of the xls file to look for the keyword
- c = int(input("In which column do you want to search (integer)?: "))
- #Open .xls file, select worksheet and create workbook for matches
- workbook = load_workbook(excelpath)
- worksheet = workbook.active
- wb_matches = Workbook()
- ws_matches = wb_matches.active
- #Print keyword out in ws_matches
- if btext == False:
- ws_matches.cell(row = 1, column = 1).value = key
- #Number of matches
- nfound = 0
- #IF KEYWORD
- if btext == False:
- #Iterate over each cell in the column
- for i in range(1, worksheet.max_row + 1):
- #Create cursor for reading the column
- cursor = worksheet.cell(row = i, column= c)
- #Temporary variable to store content of cursor
- cellcontent = str.lower(cursor.value)
- #Check if keyword is found within the cell, if true, print into new worksheet
- if match(key, cellcontent) == True:
- ws_matches.cell(row = 2 + nfound, column = 1).value = cellcontent
- nfound = nfound + 1
- #IF TEXTFILE
- elif btext == True:
- #Split each line of textfile into a list
- file = open(txtfile, 'r')
- #Keywords in list
- for line in file:
- keywordlist = file.read().splitlines()
- nkeywords = len(keywordlist)
- print(keywordlist)
- print(nkeywords)
- #Iterate over each string in list, look for match in .xlsx file
- for i in range(1, nkeywords):
- nfound = 0
- ws_matches.cell(row = 1, column = i).value = str.lower(keywordlist[i-1])
- for j in range(1, worksheet.max_row + 1):
- cursor = worksheet.cell(row = j, column = c)
- cellcontent = str.lower(cursor.value)
- if match(keywordlist[i-1], cellcontent) == True:
- ws_matches.cell(row = 2 + nfound, column = i).value = cellcontent
- nfound = nfound + 1
- wb_matches.save("matches.xlsx")
- def match(keyword, content):
- """Check if the keyword is present within the cell content, return True if found, else False"""
- if content.find(keyword) == -1:
- return False
- else:
- return True
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement