python如何读取excel宏_从python运行excel宏

我正在使用以下代码从Python运行一个Excel宏:import pymysql

import datetime

import csv

import math

import os

import glob

import sys

import win32com.client

import numpy

from tkinter import *

from tkinter import ttk

import tkinter.messagebox

def run_macro():

print('macro')

#this if is here because if an executable is created, __file__ doesn't work

if getattr(sys, 'frozen', False):

name = (os.path.dirname(sys.executable) + '\\Forecast template.xlsm')

else:

name = str(os.path.dirname(os.path.realpath(__file__)) + '\\Forecast template.xlsm')

print(name)

#this part runs the macro from excel

if os.path.exists(name):

xl=win32com.client.Dispatch("Excel.Application")

xl.Workbooks.Open(Filename=name, ReadOnly=1)

xl.Application.Run("ThisWorkbook.LoopFilesInFolder")

xl.Application.Quit() # Comment this out if your excel script closes

del xl

print('File refreshed!')

我似乎对此有一定的问题,运行此命令后,我将打开任何excel文件,只会看到一个灰色窗口:

w5hlE.png

知道为什么会这样吗?另外,如何在代码中添加一些内容以便在Excel中打开文件?(不是要获取信息,而是在Excel中打开该文件)

额外问题:如何使它不关闭所有打开的Excel文件?

编辑:我刚刚检查了宏,这很好,问题似乎只是源于我运行代码的时候。

新编辑:

这是宏中的代码:Sub LoopFilesInFolder()

Dim wb1 As Workbook

Dim wb2 As Workbook

Dim path As String

Dim file As String

Dim extension As String

Dim myFileName As String

Application.ScreenUpdating = False

Application.DisplayAlerts = False

Set wb1 = ActiveWorkbook

path = ActiveWorkbook.path & "\csvs\"

extension = "*.csv"

file = Dir(path & extension)

Do While file <> ""

Set wb2 = Workbooks.Open(Filename:=path & file)

wb2.Activate

'this section is for the avail heads file, basically it just opens it and copies the info to the template

If wb2.Name = "avail_heads.csv" Then

Range("A1").Select

Range(Selection, Selection.End(xlDown)).Select

Range(Selection, Selection.End(xlToRight)).Select

Selection.Copy

wb1.Activate

Worksheets("raw data").Range("B88").PasteSpecial xlPasteValues

End If

'this section is for the forecast file, basically it just opens it and copies the info to the template

If wb2.Name = "forecast.csv" Then

Range("A1").Select

Range(Selection, Selection.End(xlDown)).Select

Range(Selection, Selection.End(xlToRight)).Select

Selection.Copy

wb1.Activate

Worksheets("raw data").Range("B74").PasteSpecial xlPasteValues

End If

'this section is for the income file, basically it just opens it and copies the info to the template

If wb2.Name = "income volume.csv" Then

Range("A1").Select

Range(Selection, Selection.End(xlDown)).Select

Range(Selection, Selection.End(xlToRight)).Select

Selection.Copy

wb1.Activate

Worksheets("raw data").Range("B3").PasteSpecial xlPasteValues

End If

'this section is for the outgoing volume file, basically it just opens it and copies the info to the template

If wb2.Name = "outgoing_volume.csv" Then

Range("A1").Select

Range(Selection, Selection.End(xlDown)).Select

Range(Selection, Selection.End(xlToRight)).Select

Selection.Copy

wb1.Activate

Worksheets("raw data").Range("B36").PasteSpecial xlPasteValues

End If

'this section is for the required heads file, basically it just opens it and copies the info to the template

If wb2.Name = "required_heads.csv" Then

Range("A1").Select

Range(Selection, Selection.End(xlDown)).Select

Range(Selection, Selection.End(xlToRight)).Select

Selection.Copy

wb1.Activate

Worksheets("raw data").Range("B102").PasteSpecial xlPasteValues

End If

wb2.Close

file = Dir

Loop

'myFileName = ActiveWorkbook.path & "\forecast_for_w" & Format(Now, "ww") + 1

myFileName = ActiveWorkbook.path & "\yoda_forecast"

ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlWorkbookNormal

'MsgBox "Done!"

Application.DisplayAlerts = True

End Sub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值