PyInstallerで詳細タブのプロパティを指定する(pyinstaller-versionfile)

exeファイルの詳細タブ

pyinstaller-versionfileを使ってexeファイルの詳細情報を簡単に管理できるようにしようというお話です。

PyInstallerではバージョンファイルを引き渡せば詳細タブの値を変更できるんですが、直接編集するには少し億劫な代物です。

VSVersionInfo(
  ffi=FixedFileInfo(
    # filevers and prodvers should be always a tuple with four items: (1, 2, 3, 4)
    # Set not needed items to zero 0. Must always contain 4 elements.
    filevers=(1,0,0,0),
    prodvers=(1,0,0,0),
    # Contains a bitmask that specifies the valid bits 'flags'r
    mask=0x3f,
    # Contains a bitmask that specifies the Boolean attributes of the file.
    flags=0x0,
    # The operating system for which this file was designed.
    # 0x4 - NT and there is no need to change it.
    OS=0x40004,
    # The general type of file.
    # 0x1 - the file is an application.
    fileType=0x1,
    # The function of the file.
    # 0x0 - the function is not defined for this fileType
    subtype=0x0,
    # Creation date and time stamp.
    date=(0, 0)
    ),
  kids=[
    StringFileInfo(
      [
      StringTable(
        u'040904B0',
        [StringStruct(u'CompanyName', u'My Company'),
        StringStruct(u'FileDescription', u'GUIソフト'),
        StringStruct(u'FileVersion', u'1.0.0.0'),
        StringStruct(u'InternalName', u'MY APP'),
        StringStruct(u'LegalCopyright', u'© 2023 voltaney'),
        StringStruct(u'OriginalFilename', u'MY APP.exe'),
        StringStruct(u'ProductName', u'MYAPP'),
        StringStruct(u'ProductVersion', u'1.0.0.0')])
      ]), 
    VarFileInfo([VarStruct(u'Translation', [0, 1200, 1041, 1200])])
  ]
)

そこで、pyinstaller-versionfileの出番です。

pyinstaller-versionfile

Install

pip install pyinstaller-versionfile

Usage

設定ファイル

まず、詳細情報を定義するyamlファイルを用意してあげます。 ファイル名は全て自由です。

Version: version.txt
CompanyName: My Company
FileDescription: GUIソフト
InternalName: MY APP
LegalCopyright: © 2023 voltaney
OriginalFilename: MY APP.exe
ProductName: MYAPP
Translation:
  - langID: 0
    charsetID: 1200
  - langID: 1041
    charsetID: 1200

定義したくない項目は削除するか、valueに何も記載しなければOKです。 ここで、Versionversion.txtと記入していますが、この項目にファイル名を指定するとそのファイルからバージョン情報を取ってきてくれます。 もちろん直接1.0.0等と書いても問題ありません。ここではバージョンのみ別のファイルで管理します。 Translation項目に設定できる値はMSのドキュメントで確認できます。

  • version.txt
1.0.0

バージョンファイルの生成

先程のyamlファイルと出力先のファイル名を指定してバージョンファイルを生成します。 なお、--version引数を指定することで、コマンド発行時にバージョンを指定することも可能です。

create-version-file version.yaml --outfile app.version

PyInstallerの実行

PyInstallerにバージョンファイルを渡してあげます。

pyinstaller main.py --noconsole --onefile --name="MY APP" --version-file=app.version

出力されるexeファイルを確認すると、下記のようになっていると思います。

詳細タブ

一度PyInstallerを実行すれば.specファイルができあがるので、あとはpyinstaller ファイル名.specをするだけで、app.versionファイルを見に行ってくれます。すばらしい!