-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetPdfRes.py
More file actions
79 lines (67 loc) · 2.13 KB
/
Copy pathgetPdfRes.py
File metadata and controls
79 lines (67 loc) · 2.13 KB
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
#!/usr/bin/env python3
# pip3 install pymupdf
import fitz
import sys
import re
import os
def pdf2pic(pdf_path, output_path):
# 使用正则表达式来查找图片
checkXO = r"/Type(?= */XObject)"
checkIM = r"/Subtype(?= */Image)"
# 打开pdf
doc = fitz.open(pdf_path)
# 图片计数
imgcount = 0
lenXREF = doc._getXrefLength()
# 打印PDF的信息
print("文件名:{}, 页数: {}, 对象: {}".format(pdf_path, len(doc), lenXREF - 1))
# 遍历每一个对象
for i in range(1, lenXREF):
# 定义对象字符串
text = doc._getXrefString(i)
isXObject = re.search(checkXO, text)
# 使用正则表达式查看是否是图片
isImage = re.search(checkIM, text)
# 如果不是对象也不是图片,则continue
if not isXObject:
continue
if not isImage:
continue
imgcount += 1
# 根据索引生成图像
pix = fitz.Pixmap(doc, i)
# 根据pdf的路径生成图片的名称
new_name = "%02d.png" %imgcount
# 如果pix.n<5,可以直接存为PNG
if pix.n < 5:
pix.writePNG(os.path.join(output_path, new_name))
# 否则先转换CMYK
else:
pix0 = fitz.Pixmap(fitz.csRGB, pix)
pix0.writePNG(os.path.join(output_path, new_name))
pix0 = None
# 释放资源
pix = None
print("提取了{}张图片".format(imgcount))
def pdf2txt(pdf_path,output_path):
'''
从PDF中获取文本
'''
doc = fitz.open(pdf_path)
output_file = output_path + os.sep + 'output.txt'
print(output_file)
out = open(output_file,'w')
for d in doc:
out.write(d.getText("text")) # 可选 html / xml /json 等
out.close()
if __name__=='__main__':
# pdf路径
if len(sys.argv) == 1:
exit(-1)
pdf_path = sys.argv[1]
output_path = os.getcwd() + os.sep + os.path.basename(pdf_path)[:-4]
# 不存在则创建
if not os.path.exists(output_path):
os.mkdir(output_path)
pdf2txt(pdf_path,output_path)
pdf2pic(pdf_path,output_path)