Python脚本没有在excel M中选择正确的日期

2024-09-28 22:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在excel中运行一个宏,它每月生成报告宏调用python脚本时没有任何问题。但是,无论我在宏中选择哪个月,python脚本总是在一月运行。你知道吗

这是我选择月份的地方: This is where i select the month.

当python脚本运行时,总是在一月: When the python script runs it is always January.

下面是python脚本中的代码。你知道吗

#Read the month and year selected in the Monthly Report
pathOfMonthlyReportFile = os.sep.join((os.path.expanduser("~"), 
'Desktop'))+'\Monthly Activity Report - Template.xlsm'
wb = openpyxl.load_workbook(pathOfMonthlyReportFile, keep_vba=True)
sheet = wb.get_sheet_by_name('Macro')
MonthOfMonthlyReport = sheet['G10'].value
YearOfMonthlyReport = sheet['J10'].value
print(MonthOfMonthlyReport)

if MonthOfMonthlyReport == "January":
 MonthNumber = 1
elif MonthOfMonthlyReport == "February":
 MonthNumber = 2
elif MonthOfMonthlyReport == "March":
 MonthNumber = 3
elif MonthOfMonthlyReport == "April":
 MonthNumber = 4
elif MonthOfMonthlyReport == "May":
 MonthNumber = 5
elif MonthOfMonthlyReport == "June":
 MonthNumber = 6
elif MonthOfMonthlyReport == "July":
 MonthNumber = 7
elif MonthOfMonthlyReport == "August":
 MonthNumber = 8
elif MonthOfMonthlyReport == "September":
 MonthNumber = 9
elif MonthOfMonthlyReport == "October":
 MonthNumber = 10
elif MonthOfMonthlyReport == "November":
 MonthNumber = 11
elif MonthOfMonthlyReport == "December":
 MonthNumber = 12

MonthlyReportDate = 
now.replace(month=MonthNumber,year=int(YearOfMonthlyReport))
if MonthNumber == 12:
last_day_of_month = MonthlyReportDate.replace(day = 31, month=MonthNumber, 
year=int(YearOfMonthlyReport))
else:
last_day_of_month = MonthlyReportDate.replace(day=1, month=MonthNumber+1, 
year=int(YearOfMonthlyReport)) - datetime.timedelta(days=1)

if MonthNumber >= 10:
    stringenddate = "%s-%s-%s" % (MonthlyReportDate.year, 
MonthlyReportDate.month,last_day_of_month.day)
    stringstartdate = "%s-%s-01" % (MonthlyReportDate.year, 
MonthlyReportDate.month)
else:
    stringenddate = "%s-0%s-%s" % (MonthlyReportDate.year, 
MonthlyReportDate.month,last_day_of_month.day)
    stringstartdate = "%s-0%s-01" % (MonthlyReportDate.year, 
MonthlyReportDate.month)

print(stringenddate)

MonthRange = [1,2,3,4,5,6,7,8,9,10,11,12]
ThreeMonthsAgo = 0

for i in range(len(MonthRange)):
   if MonthRange[i] == MonthNumber:
    ThreeMonthsAgo = MonthRange[i-2] #**************
print(ThreeMonthsAgo)

if ThreeMonthsAgo<MonthNumber:
if ThreeMonthsAgo >=10:
    stringthreemonthsagodate = "%s-%s-01"%(MonthlyReportDate.year, 
ThreeMonthsAgo)
else:
    stringthreemonthsagodate = "%s-0%s-01"%(MonthlyReportDate.year, 
ThreeMonthsAgo)

if ThreeMonthsAgo > MonthNumber:
if ThreeMonthsAgo >=10:
    stringthreemonthsagodate = "%s-%s-01"%(MonthlyReportDate.year - 1, 
ThreeMonthsAgo)
else:
    stringthreemonthsagodate = "%s-0%s-01"%(MonthlyReportDate.year - 1, 
ThreeMonthsAgo)

date_ranges = [(stringstartdate,
                stringenddate)]
data_ranges_for_three_months = [(stringthreemonthsagodate, stringenddate)]

它应该在所选月份运行,但只在一月运行。 我真的没有得到一个错误消息,但它只是不是正确的一个月。你知道吗


已经修好了。这个解决方案有点尴尬,但我会发帖的一切都好我要做的是,每当我在宏中更改月份时,我都需要保存.xlsm,然后运行它。你知道吗


Tags: 脚本ifyearsheetlastelifdaymonth
2条回答

看起来是缓存/过时数据问题。如果我理解正确,您是否在工作簿中运行VBA宏,该宏调用python脚本,然后查看同一工作簿中的单元格?不是一个理想的结构!你知道吗

当python脚本调用Excel工作表时,请执行以下操作:

wb = openpyxl.load_workbook(pathOfMonthlyReportFile, keep_vba=True)

我想这是在看你上次保存文件时G10的值。也许最后一次储蓄的月份是一月?你知道吗

我会:

  1. 重新构造python函数,以便在从VBA调用函数时使用month和date参数并传递这些值

  2. 尝试在VBA调用python脚本之前让VBA保存您的工作表,这样就可以保留对月份和日期下拉列表所做的任何更改。

请注意1。它比2更健壮、更专业,但实现起来可能稍微复杂一些。你知道吗

在Excel电子表格中运行以下代码:

Sub TestMe()
    MsgBox Worksheets("Macro").Range("G10")
End Sub

如果您没有得到“March”,那么您很可能没有引用G10,而是引用其他内容。 此外,您正在通过启用VBA的Python打开excel文件。你确定,每当你打开它,它不会重置回January?这被称为Event()。你知道吗

相关问题 更多 >