-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
261 lines (218 loc) · 8.34 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import cmd
import json
import pdfplumber
import os
import shutil
import pandas as pd
import shutil
import zipfile
class FapiaoShell(cmd.Cmd):
""" 发票 """
intro = '欢迎使用发票提取工具,输入?(help)获取帮助消息和命令列表,CTRL+C退出程序。\n'
prompt = '\n输入命令: '
doc_header = "详细文档 (输入 help <命令>):"
misc_header = "友情提示:"
undoc_header = "没有帮助文档:"
nohelp = "*** 没有命令(%s)的帮助信息 "
path = os.path.abspath('.')
is_taxation = path+'\\result\\is_taxation'
isnot_taxation = path + '\\result\\isnot_taxation'
result_xlsx = path + '\\result\\result.xlsx'
def __init__(self):
super().__init__()
def do_load(self, arg):
""" 加载发票 例如:load D:\ """
if not os.path.isdir(arg):
print('参数必须是目录!')
return
if os.path.exists(self.result_xlsx):
os.remove(self.result_xlsx)
if os.path.exists(self.is_taxation):
shutil.rmtree(self.is_taxation)
os.mkdir(self.is_taxation)
if os.path.exists(self.isnot_taxation):
shutil.rmtree(self.isnot_taxation)
os.mkdir(self.isnot_taxation)
if not os.path.exists(self.is_taxation):
os.mkdir(self.is_taxation)
if not os.path.exists(self.isnot_taxation):
os.mkdir(self.isnot_taxation)
self.zip_file_path = 'C:\\Users\\haoci\\Desktop\\'+os.path.basename(arg)+'解析'
os.chdir(os.path.dirname(arg))
pdfs = []
for root, _, files in os.walk(arg):
for fn in files:
ext = os.path.splitext(fn)[1].lower()
if ext != '.pdf':
continue
fpth = os.path.join(root, fn)
fpth = os.path.relpath(fpth)
print(f'发现pdf文件: {fpth}')
pdfs.append(fpth)
pdf_ctxs = self._parse_pdfs(pdfs)
# total = {
# '内容': pdf_ctxs,
# '发票数': len(pdf_ctxs),
# '总计': 0,
# }
# for fpth, info in pdf_ctxs:
# total['总计'] += float(info['总计'])
df = pd.DataFrame(pdf_ctxs)
df.to_excel(self.result_xlsx, sheet_name='data')
# with open(self.path+'\\result.json', 'w', encoding='utf-8') as json_file:
# json.dump(pdf_ctxs,
# json_file,
# ensure_ascii=False,
# sort_keys=True,
# indent=4,
# separators=(', ', ': '))
# print('\n已保存到 result.json和result.xlsx...')
print('\n已保存到result.xlsx...')
try:
shutil.make_archive(self.zip_file_path, 'zip', self.path+'\\result')
print(f"压缩成功,生成的zip文件位于 {self.zip_file_path}")
except Exception as e:
print(f"压缩文件时出错: {e}")
def _parse_pdfs(self, pdfs):
""" 分析 """
result = []
for fpth in pdfs:
info = {}
info['文件名'] = fpth
with pdfplumber.open(fpth) as pdf:
page = pdf.pages[0]
if '增值税电子普通发票' not in ''.join(page.extract_text()):
result.append((fpth, {}))
inf = self._extrace_from_words(page.extract_words())
info.update(inf)
inf = self._extrace_from_table(page.extract_tables()[0],fpth)
info.update(inf)
result.append(info)
return result
def _extrace_from_words(self, words):
""" 从单词中提取 """
info = {}
lines = {}
for word in words:
top = int(word['top'])
bottom = int(word['bottom'])
pos = (top + bottom) // 2
text = word['text']
if pos not in lines:
lines[pos] = [text]
else:
lines[pos].append(text)
lines_pack = []
last_pos = None
for pos in sorted(lines):
arr = lines[pos]
if len(lines_pack) > 0 and pos - last_pos <= 10:
lines_pack[-1] += arr
continue
lines_pack.append(arr)
last_pos = pos
continue
for pack in lines_pack:
for idx, line in enumerate(pack):
if '电子普通发票' in line:
info['标题'] = line
continue
if '发票代码:' in line:
info['发票代码'] = line.split(':')[1]
continue
if '发票号码:' in line:
info['发票号码'] = line.split(':')[1]
continue
if '开票日期:' in line:
# year = line.split(':')[1]
# month = [ln for ln in pack if ln.isdigit()][0]
# day = [ln[:2] for ln in pack if '日' in ln][0]
info['开票日期'] = line.split(':')[1]
continue
if '机器编号:' in line:
info['机器编号'] = [ln for ln in pack if ln.isdigit()
and len(ln) > 10][0]
continue
if '码:' in line:
c1 = pack[idx].split(':')[1]
c2 = pack[idx + 1]
c3 = pack[idx + 2]
c4 = pack[idx + 3]
info['校验码'] = f'{c1} {c2} {c3} {c4}'
continue
if '收款人:' in line:
info['收款人'] = line.split(':')[1]
continue
if '开票人:' in line:
info['开票人'] = line.split(':')[1]
continue
return info
def _extrace_from_table(self, table, fpth):
""" 从表中提取 """
info = {}
if len(table) != 4:
return None
# 购买方
for cell in table[0]:
if not cell:
continue
lines = cell.splitlines()
for line in lines:
if '名 称:' in line:
info['购买方名称'] = line.split(':')[1]
continue
# if len(line) == 18 and line.isalnum():
# info['购买方税号'] = line
# continue
#
# if len(line) == 27:
# if '密码' not in info:
# info['密码'] = []
# info['密码'].append(line)
# continue
# 详细
for cell in table[1]:
if not cell:
continue
lines = cell.splitlines()
for line in lines:
if '金 额' in line:
info['金额'] = lines[-1][1:]
break
if '税 额' in line:
info['税额'] = lines[-1][1:]
break
if '税率' in line:
# if '税率' in line and not info['购买方名称'] ==' 临沂申通快递有限公司':
# if '税率' in line and info['购买方名称'] == ' 临沂罗达智能物流有限公司':
if lines[-1] == '不征税':
shutil.copy(fpth, self.isnot_taxation)
else:
shutil.copy(fpth, self.is_taxation)
break
# 合计
for cell in table[2]:
if not cell:
continue
lines = cell.splitlines()
for line in lines:
if '¥' in line:
info['总计'] = line[1:]
# 销售方
for cell in table[3]:
if not cell:
continue
lines = cell.splitlines()
for line in lines:
if '名 称:' in line:
info['销售方名称'] = line.split(':')[1]
continue
if len(line) == 18 and line.isalnum():
info['销售方税号'] = line
continue
return info
if __name__ == '__main__':
try:
FapiaoShell().cmdloop()
except KeyboardInterrupt:
print('\n\n再见!')