Finding next month: Excel, Python

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
899
Hello the following returns "Feb 25"

today = datetime.datetime.today().date()
string_date = today.strftime("%b %y")

I now want to adapt so that it finds the next month from today, "Mar 25".

Any ideas, thanks.
 
Sorry, I must have typed that in error.

All I want to be able to do is refer to the cell. As shown in the code below.

Thanks Mark.

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import datetime
import matplotlib.dates as mdates
import numpy as np
import matplotlib.ticker as plticker
from dateutil.relativedelta import relativedelta
import calendar

today = datetime.datetime.today().date()
string_date = Cell T1 (which is: Mar 25)

df["Date Captured"] = pd.to_datetime(df["Date Captured"])
df["Check-In"] = pd.to_datetime(df["Check-In"])
df_filtered = df[(df["Date Captured"].dt.date == today) & (df["Mon Year"] == string_date)]
grouped = df_filtered.groupby("Check-In")["Current price"].min().reset_index()
 
Upvote 0
Using openpyxl it would just be

Python:
wb = Workbook()

sheet = wb.active

YOURVARIABLE = sheet['T1'].Value
 
Last edited:
Upvote 0
Thanks, I've tried this and I receive this error when referring to cell T1.

AttributeError: 'Cell' object has no attribute 'Value'

Any idea?
 
Upvote 0
Using openpyxl

Python:
from openpyxl import load_workbook

yourWb = load_workbook(filename='YourFile.xlsx')

yourWs = yourWb['Sheet1']

YourString = yourWs['T1'].Value
 
Upvote 0
This is my entire code; it plots a bar chart in the spreadsheet I'm working. Cell "T1" is in sheet named "Visuals"

I've added in your suggestions. Hopefully this helps...

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import datetime
import matplotlib.dates as mdates
import numpy as np
import matplotlib.ticker as plticker
from dateutil.relativedelta import relativedelta
import calendar
import openpyxl
from openpyxl import load_workbook

yourWb = load_workbook(filename='Python Testing.xlsm')
yourWs = yourWb['Visuals']

today = datetime.datetime.today().date()
string_date = yourWs['T1']
long_string_date = "March 2025"

df["Date Captured"] = pd.to_datetime(df["Date Captured"])
df["Check-In"] = pd.to_datetime(df["Check-In"])
df_filtered = df[(df["Date Captured"].dt.date == today) & (df["Mon Year"] == string_date)]
grouped = df_filtered.groupby("Check-In")["Current price"].min().reset_index()

fig, ax = plt.subplots()
p1 = ax.bar(grouped["Check-In"], grouped["Current price"], color='g')
ax.bar_label(p1, label_type="edge")
ax.bar_label(p1)

# Formatting the x-axis
ax.xaxis.set_major_formatter(mdates.DateFormatter("%a %d")) # Format as "Jan 2025"
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
plt.xlabel("Check-In Date")
plt.ylabel("Price (£)")
plt.title(long_string_date)
plt.xticks(rotation=90) # Rotate labels
plt.ylim(50, 200)
plt.show()
 
Upvote 0
Openpyxl isn't compliant with xlsm, I think you need to do some reading up on the libraries and Python programming as it will help you more than forums
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top