Last active
September 1, 2023 04:41
-
-
Save y-ookuma/c04d61ff7ca3e9513ee9a3779a1ce568 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#----------------------------------------------------------- | |
# 20230901 | |
# Amazonの領収書のpdfのファイル名を | |
# "注文日付_請求金額_Amazon.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) | |
accounting_day=extraction_string(input_string,"注文日:","\n") | |
accounting_amount_money=extraction_string(input_string,"¥","\n") | |
accounting_day=accounting_day.replace(" ","") | |
accounting_amount_money= accounting_amount_money.replace(",","") | |
accounting_amount_money= accounting_amount_money.zfill(7) #百万まで左0詰め | |
date_object = datetime.strptime(accounting_day, "%Y年%m月%d日") | |
formatted_date = date_object.strftime("%Y%m%d") | |
print(formatted_date+"_"+accounting_amount_money+"_Amazon.pdf") | |
new_filename = formatted_date+"_"+accounting_amount_money+"_Amazon.pdf" | |
# ファイル名を変更 | |
os.rename(pdf, new_filename) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment