@frswataru (本石 渉)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ExcelVBAでPythonファイル実行したい

解決したいこと

ExcelVBAでPythonファイル実行出来したいです
下記のようなエラーが発生しました。
解決方法を教えて下さい。

ディレクトリー

desktop─GDT┬GDT.xlsm
”””””””””””””””” └GDT.py

発生している問題・エラー

---------------------------
Error
---------------------------
Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File "c:\users\* *\desktop\gdt\GDT.py", line 10

    def GDT():

    ^

IndentationError: unexpected indent



Press Ctrl+C to copy this message to the clipboard.
---------------------------
OK   
---------------------------

GDT.xlsm

Sub Run_Python()
    RunPython ("import GDT;GDT.GDT()")
End Sub

GDT.py

#!/usr/bin/env python
# coding: utf-8

# In[5]:


import pyaudio
import wave
import time
 def GDT():

DEVICE_INDEX = 0
CHUNK = 1024
FORMAT = pyaudio.paInt16 # 16bit
CHANNELS = 1             # monaural
RATE = 48000             # sampling frequency [Hz]

time = 3 # record time [s]       
output_path = "./sample.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                input_device_index = DEVICE_INDEX,
                frames_per_buffer=CHUNK)

print("recording ...")

frames = []

for i in range(0, int(RATE / CHUNK * time)):
    data = stream.read(CHUNK)
    frames.append(data)

print("done.")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(output_path, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()


# In[6]:


import speech_recognition as sr

r = sr.Recognizer()

with sr.AudioFile("sample.wav") as source:
    audio = r.record(source)

text = r.recognize_google(audio, language='ja-JP')

print(text)


# In[ ]:





0 likes

1Answer

def GDT():

行頭に空白があります。それが
unexpected indent
つまり、あってはならない位置にインデントがあるというエラーです。

それ以降の行では逆にインデントが必要と思われるのに無いようです。
Pythonはインデントが重要な意味を持ちます。
それを理解してコードを書き直す必要があります。

2Like

Your answer might help someone💌