Advertisement
MyNamesNotReallyDave

u/aggiefury101 - full

Feb 11th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. import csv
  2. import json
  3. import time
  4. import os
  5. import pandas as pd
  6. from selenium import webdriver
  7.  
  8. # Load Chrome Driver
  9. CHROMEDRIVER_PATH = 'C:/Users/rgraham_adm/Desktop/WebScraper2020/chromedriver.exe'
  10.  
  11. # ------ changes ------
  12. # Names to Be Renamed - This is referenced in the renaming loop
  13. newNames = 'C:/Users/rgraham_adm/Desktop/RedditScript/Forms/NewNames.csv'
  14.  
  15. with open('newNames', 'r') as f:
  16.     reader = csv.reader(f)
  17.     newNameList = list(reader)
  18. # ------ /changes ------
  19.  
  20. appState = {
  21.     "recentDestinations": [
  22.         {
  23.             "id": "Save as PDF",
  24.             "origin": "local",
  25.             "account": ""
  26.         }
  27.     ],
  28.     "selectedDestinationId": "Save as PDF",
  29.     "version": 2
  30. }
  31.  
  32. downloadPath = 'C:/Users/rgraham_adm/Desktop/RedditScript/Forms'
  33.  
  34. profile = {'printing.print_preview_sticky_settings.appState': json.dumps(appState),
  35.            'savefile.default_directory': downloadPath}
  36.  
  37. options = webdriver.ChromeOptions()
  38. options.add_experimental_option('prefs', profile)
  39. options.add_argument('--kiosk-printing')
  40. driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
  41. with open('C:/Users/rgraham_adm/Desktop/RedditScript/Test.csv') as example_file:
  42.     example_reader = csv.reader(example_file)
  43.     for row in example_reader:
  44.         driver.get(row[0]), time.sleep(5), driver.execute_script('window.print();')
  45.  
  46. driver.quit()
  47.  
  48. # ------ changes ------
  49. for index, item in enumerate(os.listdir(downloadPath):
  50.     """Assuming that the CSV with your new file names:
  51.    a. is in the correct order / doesn't require an order,
  52.    b. contains a column with the new file names in string format, without file extensions
  53.    this should work. The second item in the rename method uses the following syntax
  54.    to determine the new filename:
  55.    
  56.    newNames - [index] - [0]
  57.    name of the filepath variable - number of the file to find the corresponding line - number of the column containing the string
  58.    """
  59.     os.rename(f'{downloadPath}/{item}', f'{downloadPath}/{newNames[index][0]}.pdf')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement