-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPython for PDF code.py
136 lines (53 loc) · 1.3 KB
/
Python for PDF code.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
# coding: utf-8
# ## PyPDF2
# In[49]:
get_ipython().system('pip install PyPDF2')
# In[50]:
import PyPDF2
# In[51]:
#!dir
pdfFileObj = open('sample.pdf', 'rb')
# In[52]:
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# In[53]:
print(pdfReader.numPages)
# In[54]:
pageObj = pdfReader.getPage(1)
# In[55]:
# creating a page object
# extracting text from page
print(pageObj.extractText())
# In[56]:
# of you want to store data into String
Pdf_String=pageObj.extractText()
# In[57]:
Pdf_String
# ## tabula
# ##### for Tabular Data
# In[58]:
get_ipython().system('pip install tabula-py')
# In[59]:
import tabula
# In[60]:
df = tabula.read_pdf("offense.pdf")
# In[62]:
df.head()
# #### for Mltiple Table
# In[63]:
df = tabula.read_pdf("data.pdf",multiple_tables=True)
# In[66]:
df
# ### For specific part
# In[67]:
tabula.read_pdf("data.pdf", area=(126,149,212,462), pages=1)
# #### Json as Output Format
# In[70]:
tabula.read_pdf("data.pdf", output_format="json")
# ### Export into Excel or CSV
# In[71]:
tabula.convert_into("data.pdf", "data_excel.xlsx", output_format="xlsx")
# this will create data_excel.xlsx
# In[72]:
# for CSV export
tabula.convert_into("data.pdf", "data_csv.csv", output_format="csv")
# this will create data_excel.csv