1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
// 電子帳簿保存法なにやらで pdf ファイルが増えて // 何のファイルか分からなくなるので // 取り敢えず、自動でファイル名を変更してみる // // pdftotext を利用して pdf からテキストを取り出しそれを利用してファイル名を変更する // 今回は以下のファイルを対象にしている // 変更前: foo{数字}.pdf // 変更後: foo_{受付時刻}_{受付時刻}_{振込指定日}_{受取人}_{信用金庫名}_{支店名}.pdf package main import ( "fmt" "os" "os/exec" "path/filepath" "strconv" "strings" "github.com/dlclark/regexp2" ) type Name struct { bank string branch string reception string specified string receiver string } func main() { for i := 1; i < len(os.Args); i++ { arg := os.Args[i] dir := filepath.Dir(arg) basename := filepath.Base(arg) ext := filepath.Ext(arg) // fname := basename[0 : len(basename)-len(ext)] if strings.ToLower(ext) != ".pdf" { continue } if strings.Index(strings.ToLower(basename), "foo") != -1 { text, err := pdftotext(arg) if err != nil { fmt.Println(err) continue } // fmt.Println(i, dir, fname, ext, text) // fmt.Println("=====================") name := foo(text) newName := filepath.Join(dir, fmt.Sprintf("foo_%s_%s_%s_%s_%s%s", name.reception, name.specified, name.receiver, name.bank, name.branch, ext)) if Exists(newName) { continue } fmt.Println(arg, "==>", newName) if err = os.Rename(arg, newName); err != nil { fmt.Println(err) } } } } func pdftotext(pdf string) (text string, err error) { var out []byte out, err = exec.Command("pdftotext", pdf, "-").Output() if err != nil { return } text = string(out) return } func foo(text string) (name Name) { // Bank // re := regexp2.MustCompile(`(?m)^.*信用金庫`, 0) re := regexp2.MustCompile(`.*信用金庫`, 0) m, _ := re.FindStringMatch(text) bank := strings.TrimSpace(m.String()) // Branch re = regexp2.MustCompile(`お取引店[\s\n]+(.*)`, 0) m, _ = re.FindStringMatch(text) gps := m.Groups() branch := strings.TrimSpace(gps[1].Captures[0].String()) // Reception datetime re = regexp2.MustCompile(`受付時刻[\s\n]+(\d+)年(\d+)月(\d+)日(\d+)時(\d+)分(\d+)秒`, 0) m, _ = re.FindStringMatch(text) gps = m.Groups() reception := fmt.Sprintf("%04d-%02d-%02d_%02d%02d%02d", atoi(gps[1].Captures[0].String()), atoi(gps[2].Captures[0].String()), atoi(gps[3].Captures[0].String()), atoi(gps[4].Captures[0].String()), atoi(gps[5].Captures[0].String()), atoi(gps[6].Captures[0].String())) // Specified transfer date re = regexp2.MustCompile(`振込指定日[\s\n]+(\d+)年(\d+)月(\d+)日`, 0) m, _ = re.FindStringMatch(text) gps = m.Groups() specified := fmt.Sprintf("%04d-%02d-%02d", atoi(gps[1].Captures[0].String()), atoi(gps[2].Captures[0].String()), atoi(gps[3].Captures[0].String())) // Receiver re = regexp2.MustCompile(`受取人[\s\n]+(.*)`, 0) m, _ = re.FindStringMatch(text) gps = m.Groups() receiver := strings.TrimSpace(gps[1].Captures[0].String()) /* for i := 0; i < len(gps); i++ { for j := 0; j < len(gps[i].Captures); j++ { fmt.Println(i, j, gps[i].Captures[j].String()) } } */ // fmt.Println("bank", bank, "branch", branch, "reception", reception, "specified", specified, "receiver", receiver) return Name{ bank: bank, branch: branch, reception: reception, specified: specified, receiver: receiver, } } func Exists(filename string) bool { _, err := os.Stat(filename) return err == nil } func atoi(a string) int { i, err := strconv.Atoi(a) if err != nil { panic(err) } return i } |