-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_cid_labor.py
138 lines (120 loc) · 2.38 KB
/
pdf_cid_labor.py
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
"""已知原始 pdf 和 导出后的含 (cid:xxx) 的 txt 文件。
通过对比,手工找出 (cid:xxx) 和 字符的对应关系。
目前仅适用部分英语字体
"""
import re
cmap = {
2: "’",
3: " ",
4: "A",
17: "B",
18: "C",
24: "D",
28: "E",
38: "F",
39: "G",
44: "H",
47: "I",
58: "J",
60: "K",
62: "L",
68: "M",
69: "N",
75: "O",
87: "P",
89: "Q",
90: "R",
94: "S",
100: "T",
104: "U",
115: "V",
116: "W",
121: "X",
122: "Y",
127: "Z",
258: "a",
271: "b",
272: "c",
282: "d",
286: "e",
296: "f",
297: "fb",
299: "ff",
322: "fj",
325: "fk",
332: "ft",
336: "g",
346: "h",
349: "i",
361: "j",
364: "k",
367: "l",
373: "m",
374: "n",
381: "o",
393: "p",
395: "q",
396: "r",
400: "s",
410: "t",
414: "tf",
415: "ti",
425: "tt",
426: "ttf",
427: "tti",
437: "u",
448: "v",
449: "w",
454: "x",
455: "y",
460: "z",
853: ",",
855: ":",
856: ".",
859: "’",
876: "/",
882: "-",
894: "(",
895: ")",
919: '"',
926: "©",
928: "®",
1004: "0",
1005: "1",
1006: "2",
1007: "3",
1008: "4",
1009: "5",
1010: "6",
1011: "7",
1012: "8",
1013: "9",
}
CID_REGEX = re.compile(r"\(\s*cid\s*:\s*\d+\s*\)")
def gather_unknown_cid(unique_str_cids):
res = []
for str_cid in unique_str_cids:
cid = int(str_cid.split(":")[1].strip(")").strip())
if cid not in cmap or cmap[cid] is None:
res.append(str_cid)
return res
def replace_cid(match):
str_cid = match.group(0)
cid = int(str_cid.split(":")[1].strip(")").strip())
s = cmap.get(cid, str_cid)
return s
input_fn = "a.txt"
output_fn = "b.txt"
with open(input_fn, "r", encoding="utf-8") as f:
raw_content = f.read()
cid_matches = CID_REGEX.findall(raw_content)
unique_str_cids = set(cid_matches)
print(f"文件中 (cid:xxx) 集合的大小: {len(unique_str_cids)}")
unknown_cid_list = gather_unknown_cid(unique_str_cids)
if unknown_cid_list:
print("未知的 (cid:xxx) 列表:")
for str_cid in unknown_cid_list:
print(str_cid)
replaced_content = CID_REGEX.sub(replace_cid, raw_content)
with open(output_fn, "w", encoding="utf-8") as f:
f.write(replaced_content)