Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import sys, csv
  2. from openpyxl import load_workbook, Workbook
  3.  
  4. def main():
  5.    
  6.     #Ask for path to excel file
  7.     excelpath = input("Path to excel file: ")
  8.     #Ask for keyword
  9.     txtorkey = input("Type textfile for .txt or keyword for unique keyword: ")
  10.     if txtorkey == "textfile":
  11.         txtfile = input("Path to textfile: ")
  12.         btext = True
  13.     elif txtorkey == "keyword":
  14.         key = str.lower(input("keyword (single): "))
  15.         btext = False
  16.  
  17.     #Ask for in which column of the xls file to look for the keyword
  18.     c = int(input("In which column do you want to search (integer)?: "))
  19.    
  20.     #Open .xls file, select worksheet and create workbook for matches
  21.     workbook = load_workbook(excelpath)
  22.     worksheet = workbook.active
  23.     wb_matches = Workbook()
  24.     ws_matches = wb_matches.active
  25.  
  26.     #Print keyword out in ws_matches
  27.     if btext == False:
  28.         ws_matches.cell(row = 1, column = 1).value = key
  29.     #Number of matches
  30.     nfound = 0
  31.    
  32.     #IF KEYWORD
  33.     if btext == False:
  34.         #Iterate over each cell in the column
  35.         for i in range(1, worksheet.max_row + 1):
  36.             #Create cursor for reading the column
  37.             cursor = worksheet.cell(row = i, column= c)
  38.  
  39.             #Temporary variable to store content of cursor
  40.             cellcontent = str.lower(cursor.value)
  41.             #Check if keyword is found within the cell, if true, print into new worksheet
  42.             if match(key, cellcontent) == True:
  43.                 ws_matches.cell(row = 2 + nfound, column = 1).value = cellcontent
  44.                 nfound = nfound + 1
  45.     #IF TEXTFILE
  46.     elif btext == True:
  47.         #Split each line of textfile into a list
  48.         file = open(txtfile, 'r')
  49.         #Keywords in list
  50.         for line in file:          
  51.             keywordlist = file.read().splitlines()
  52.         nkeywords = len(keywordlist)    
  53.         print(keywordlist)
  54.         print(nkeywords)
  55.            
  56.         #Iterate over each string in list, look for match in .xlsx file
  57.         for i in range(1, nkeywords):
  58.             nfound = 0
  59.             ws_matches.cell(row = 1, column = i).value = str.lower(keywordlist[i-1])
  60.             for j in range(1, worksheet.max_row + 1):
  61.                 cursor = worksheet.cell(row = j, column = c)
  62.                 cellcontent = str.lower(cursor.value)
  63.                 if match(keywordlist[i-1], cellcontent) == True:
  64.                     ws_matches.cell(row = 2 + nfound, column = i).value = cellcontent
  65.                     nfound = nfound + 1      
  66.  
  67.     wb_matches.save("matches.xlsx")
  68.    
  69. def match(keyword, content):
  70.     """Check if the keyword is present within the cell content, return True if found, else False"""
  71.     if content.find(keyword) == -1:
  72.         return False
  73.     else:
  74.         return True
  75.    
  76.    
  77.  
  78. if __name__ == "__main__":
  79.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement