LoginSignup
kodama_python
@kodama_python

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

pyinstallerを使用して作成したexeファイルだとうまく動かない

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

Pythonのスクリプトファイルだとうまく実行できるが、pyinstallerを使用して作成したexeファイルだと、うまく動かない

例)
・Pythonスクリプトと同一の階層にある、複数のPDFファイルについて、一つのPDFファイルに結合したい
・そのスクリプトファイルをpyinstallerでexe化し、Python環境がない人でも、任意のフォルダで実行できるようにしたい

該当するソースコード

# 1: ライブラリ設定
import PyPDF2
import glob
import os
import datetime

now = datetime.datetime.now() # 現在時刻の取得
now = now.strftime('%Y%m%d%H%M%S')

# 2: フォルダ内のPDFファイルを取得
os.chdir(os.path.dirname(os.path.abspath(__file__)))
path = os.getcwd() 
filelist = glob.glob(path + '/*.pdf')
#print(filelist)

# 3: PDF結合オブジェクトの生成
merger = PyPDF2.PdfMerger()

# 4: PDFを1ファイルずつ結合
for file in filelist:
    merger.append(file)

# 5: オブジェクトを書き出し
filename = '結合結果' + '_' + now + '.pdf'
merger.write(os.path.join(path, filename))
merger.close()

cmdで実行したが、エラーも出ず、何も起きない

C:\Users\750015>C:\Users\750015\Desktop\出力キャッシュレス\同一フォルダ内のPDFを結合する.exe

C:\Users\750015>

何卒ご教授のほどお願いいたします。
Windows環境です。

0

2Answer

pythonのバージョン・exe化の手法で異なる結果になるが、おおよそ実行中のファイルのパスが違うのが原因

確認した環境
Python 3.8.7
pyinstaller 5.7.0

pyファイルを実行したとき

C:\Users\ユーザー名\hogehoge\Qiita.py

exeファイルを実行したとき

C:\Users\ユーザー名\AppData\Local\Temp\_MEI{数字6桁}\Qiita.py

以下、参考URL

1

Comments

  1. @kodama_python

    Questioner
    できました!!ありがとうございます!!

Comments

  1. @kodama_python

    Questioner
    ありがとうございます。
    コマンドプロンプトで実行してみましたが、エラーすら出ません、、
  2. printを仕込むか、loggerを使うかで、プリントデバッグしてみては。

    #print(filelist)

    これがまず肝でしょうね。

Your answer might help someone💌