0

I am completely buffled. i wrote a program (which executes automatically), which opens a .txt (from the previous month) file from my desktop and puts all the content in an excel worksheet and then opens the excel file. Afterwards it deletes the .txt file.

It worked last month (because i have an excel entry), but it somehow stopped finding the .txt file (i generated a file by hand to test it).

from datetime import date, timedelta
import pandas as pd
import numpy as np
import openpyxl
from openpyxl import load_workbook
import os


def txt_to_excel_transformer():
    now = date.today()
    last_month = now.month-1 if now.month > 1 else 12
    last_year = now.year - 1


work='C:\\Users\\sebii\\Dropbox\\Programmier_Projekte\\Telegram_bot_Ausgabenliste\\Ausgabenliste.xlsx'



    if last_month != 1:
        #Windows PC
        filename2 = "C:\\Users\\sebii\\Desktop\\" + "0" + str(last_month) + "." + str(now.year) + ".txt"
        way = "0" + str(last_month) + "." + str(now.year) + ".txt"
        sheet = "0" + str(last_month) + "." + str(now.year)
        #Raspberry Pi
        #filename2 = "Y:\\" + "0" + str(last_month) + "." + str(now.year) + ".txt"
        #way = "0" + str(last_month) + "." + str(now.year) + ".txt"
        #sheet = "0" + str(last_month) + "." + str(now.year)
    
    
    
    else:
        #Windows PC
        filename2 = "C:\\Users\\sebii\\Desktop\\" +  "12" + "." + str(last_year) 
        way = "12" + "." + str(last_year) + ".txt"
        sheet = "12" + "." + str(last_year)
        #Raspberry Pi
        #filename2 = "Y:\\skripte\\" +  "12" + "." + str(last_year) 
        #way = "12" + "." + str(last_year) + ".txt"
        #sheet = "12" + "." + str(last_year)
    
    print(way)

    try:
        df = pd.read_csv(filename2, sep=" ", error_bad_lines=False)
    
        # Load existing Excel file
        book = load_workbook(work)
        writer = pd.ExcelWriter(work, engine = 'openpyxl')
        # Starting with the existing worksheets
        writer.book = book
        # Adding a new worksheet and storing the data in it
        df.to_excel(writer, sheet_name=sheet)
        writer.save()




        os.remove(filename2)
        os.startfile(work)
    
    except:
        print("Es ist keine alte Datei vorhanden")
 

txt_to_excel_transformer()

enter code here

  • You said the text file was manually created and the program deletes the text file after making an entry in the spreadsheet. Did you create it again? – Chandral Oct 15 '20 at 04:24
  • 1
    What do you mean _"somehow it stopped finding the file"_? Are you sure it isn't a different error? [Edit] your question to include the full stack trace. [Catching and suppressing all exceptions is bad practice](https://stackoverflow.com/a/54396161/843953) – Pranav Hosangadi Oct 15 '20 at 04:25
  • The .txt is normally created via a telegram bot and saved on my desktop. I just copied the file for the current month and changed the date. So it was created by a program, but edited it. The program always gives the output from my except function ("Es ist keine alte Datei vorhanden", which means no old file found) – Sebastian Schmidt Oct 15 '20 at 05:13
  • Thank you for your advice with the exception. This is the output i am getting: – Sebastian Schmidt Oct 15 '20 at 19:26

0 Answers0