Skip to content

Instantly share code, notes, and snippets.

@y-ookuma
Last active September 1, 2023 04:42
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 y-ookuma/a9ddc4b60779c8a72614f7226088d4eb to your computer and use it in GitHub Desktop.
Save y-ookuma/a9ddc4b60779c8a72614f7226088d4eb to your computer and use it in GitHub Desktop.
#-----------------------------------------------------------
# 20230901
# Monotaroの領収書のpdfのファイル名を"注文日付_領収金額_Monotaro.pdf"とする
#-----------------------------------------------------------
import glob,os
from pdfminer.high_level import extract_text
from datetime import datetime
# 文字列抽出(検索文字列、開始文字、終了文字)
def extraction_string(input_string,start_string,end_string):
start_index = input_string.find(start_string)
end_index = input_string.find(end_string, start_index)
if start_index != -1 and end_index != -1:
extracted_string = input_string[start_index + len(start_string):end_index]
print("抽出された文字列:", extracted_string)
return extracted_string
else:
print("指定された文字列が見つかりませんでした。")
return None
input_list=glob.glob("*.pdf")
print(input_list)
for pdf in input_list:
if "領収証の発行" in pdf: #まだ、ファイル名の変換がされていない場合の処理
input_string = extract_text(pdf)
# print(input_string)
accounting_day=extraction_string(input_string,"領収⽇︓","\n")
accounting_amount_money=extraction_string(input_string,"¥","\n")
# print(accounting_day,accounting_amount_money)
# accounting_day=accounting_day.replace(" ","")
accounting_amount_money= accounting_amount_money.replace(",","")
accounting_amount_money= accounting_amount_money.replace("-","")
accounting_amount_money= accounting_amount_money.zfill(7) #百万まで左0詰め
accounting_day=accounting_day.replace("年","")
accounting_day=accounting_day.replace("⽉","")
accounting_day=accounting_day.replace("⽇","")
print(accounting_day+"_"+accounting_amount_money+"_Monotaro.pdf")
new_filename = accounting_day+"_"+accounting_amount_money+"_Monotaro.pdf"
# ファイル名を変更
os.rename(pdf, new_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment