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