SpanishCoder
SpanishCoder

Reputation: 1

How to run python code when new file has been added to a folder in desktop?

I am trying to automate a process with VBA and Python. I use a VBA macro to send from my mail, pdf files to a specific folder in my desktop. Then I run a code that convert this pdf into csv file. I want to automate the process and make run the python code only in the newest pdf file uploaded.

I currently use this code for converting the file:

import tabula
import pandas as pd
tabula.convert_into(r"pathtofile.pdf", r"pathtofile.csv", pages="all")

After running the code, converted files appear in the folder. I have seen you can use a library called watchdog to monitor files changes. Despite that, i have not found a robust code for this automation,

Thanks b4hand and best regards

Upvotes: 0

Views: 158

Answers (1)

overstacked
overstacked

Reputation: 24

See if this helps:

import tabula
import pandas as pd
import os
import time

# The path to the directory you want to monitor
path_to_watch = "C:/Users/YourName/Directory/To/Monitor"
# Any file extension you want to limit by
file_type = ".pdf"

before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
  time.sleep (10)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added:
    for filename in added:
      if filename.endswith(file_type):
        tabula.convert_into(filename, filename.replace(file_type, ".csv"), pages="all")
  before = after

Upvotes: 1

Related Questions