Skip to content

Instantly share code, notes, and snippets.

@hpxEHwjQ
Last active December 28, 2018 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hpxEHwjQ/81a737cefd9c24552a27ad4ee5d395af to your computer and use it in GitHub Desktop.
Save hpxEHwjQ/81a737cefd9c24552a27ad4ee5d395af to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import docx
import sys
#日本語フォントの取得に必要??
from docx.oxml.ns import qn
#パラグラフのリストを受け取り、
#所属するrunsのフォント情報をチェックする関数
def check_paragraphs_fonts(paragraphs, fonts_result, styles_result):
#各段落のrunを順番に見ていく
for para in paragraphs:
if 0 == len(para.text):
continue
for i, run in enumerate(para.runs):
#Noneの場合は、スタイルを継承していて、フォント名はわからない
if(None is run.font.name):
if(para.style.name not in styles_result):
styles_result.append(para.style.name)
else:
if(run.font.name not in fonts_result):
fonts_result.append(run.font.name)
#終了したときにDOS窓が閉じないようにする関数
def enter2exit():
input("\nエンターを入力してください。終了します。")
sys.exit()
if __name__ == "__main__":
#実行パラメータ読む(調査対象のファイル名を引く)
if len(sys.argv) <= 1:
print("docxファイルをドロップしてください。")
enter2exit()
#結果保存用のリスト
fonts_result = []
styles_result = []
for arg in sys.argv[1:]:
print('ファイル:'+ arg)
try:
# wordファイルを開く
doc = docx.Document(arg)
except:
print('エラー、docxファイルの読み込みに失敗しました。:'+ arg)
enter2exit()
fonts_result.clear()
styles_result.clear()
#ここから本体の処理
#まず、ドキュメント配下のパラグラフを、関数でチェック
check_paragraphs_fonts(doc.paragraphs, fonts_result, styles_result)
#表組は段落リストからアクセスできないので、別に処理する
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
check_paragraphs_fonts(doc.paragraphs, fonts_result, styles_result)
#結果表示
print('\n本文、表内の書体を表示します。')
for font in fonts_result:
print(font)
print('\n本文、表内のスタイルを表示します。')
for style in styles_result:
print('style:'+style)
if(doc.styles[style].font.name is not None):
print('スタイル設定フォント:' + doc.styles[style].font.name)
else:
print('スタイル設定フォント: 不明')
print("")
enter2exit()
@hpxEHwjQ
Copy link
Author

source code for post of https://www.gentie.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment