danielhjerresen commited on
Commit
eeeaa67
·
verified ·
1 Parent(s): 9aa8bd3

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +188 -0
  2. pdf_counter.py +1 -0
app.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import streamlit as st
3
+ import fitz
4
+
5
+ from pdf_counter import count_characters
6
+
7
+
8
+ st.set_page_config(
9
+ page_title="PDF Character Counter",
10
+ layout="wide",
11
+ )
12
+
13
+ st.title("PDF Character Counter")
14
+ st.write(
15
+ "Counts characters including spaces and can automatically remove headers, footers, and page numbers."
16
+ )
17
+
18
+ uploaded_file = st.file_uploader(
19
+ "Upload PDF",
20
+ type=["pdf"],
21
+ )
22
+
23
+ if uploaded_file:
24
+ pdf_bytes = uploaded_file.read()
25
+
26
+ doc = fitz.open(
27
+ stream=pdf_bytes,
28
+ filetype="pdf",
29
+ )
30
+
31
+ page_count = len(doc)
32
+
33
+ st.subheader("Settings")
34
+
35
+ excluded_pages = st.multiselect(
36
+ "Exclude pages",
37
+ options=list(range(1, page_count + 1)),
38
+ default=[],
39
+ )
40
+
41
+ col1, col2, col3 = st.columns(3)
42
+
43
+ with col1:
44
+ remove_headers = st.checkbox(
45
+ "Remove headers",
46
+ value=True,
47
+ )
48
+
49
+ with col2:
50
+ remove_footers = st.checkbox(
51
+ "Remove footers",
52
+ value=True,
53
+ )
54
+
55
+ with col3:
56
+ remove_page_numbers = st.checkbox(
57
+ "Remove page numbers",
58
+ value=True,
59
+ )
60
+
61
+ characters_per_normal_page = st.number_input(
62
+ "Tegn pr. normalside",
63
+ min_value=1,
64
+ value=2400,
65
+ step=100,
66
+ )
67
+
68
+ result = count_characters(
69
+ pdf_bytes=pdf_bytes,
70
+ excluded_pages=set(excluded_pages),
71
+ remove_headers=remove_headers,
72
+ remove_footers=remove_footers,
73
+ remove_page_numbers=remove_page_numbers,
74
+ )
75
+
76
+ normal_pages = result["total_characters"] / characters_per_normal_page
77
+
78
+ st.divider()
79
+
80
+ col1, col2 = st.columns(2)
81
+
82
+ with col1:
83
+ st.metric(
84
+ "Characters including spaces",
85
+ f"{result['total_characters']:,}".replace(",", "."),
86
+ )
87
+
88
+ with col2:
89
+ st.metric(
90
+ "Normalsider",
91
+ f"{normal_pages:.2f}".replace(".", ","),
92
+ )
93
+
94
+ st.divider()
95
+
96
+ st.subheader("Elements removed from the count")
97
+
98
+ removed_items = result["removed_items"]
99
+
100
+ removed_headers = [
101
+ item
102
+ for item in removed_items
103
+ if item["Type"] in ["Sidehoved", "Løbende sidehoved"]
104
+ ]
105
+
106
+ removed_footers = [
107
+ item
108
+ for item in removed_items
109
+ if item["Type"] == "Sidefod"
110
+ ]
111
+
112
+ removed_page_numbers = [
113
+ item
114
+ for item in removed_items
115
+ if item["Type"] == "Sidetal"
116
+ ]
117
+
118
+ col1, col2, col3 = st.columns(3)
119
+
120
+ with col1:
121
+ st.metric(
122
+ "Headers removed",
123
+ len(removed_headers),
124
+ )
125
+
126
+ with col2:
127
+ st.metric(
128
+ "Footers removed",
129
+ len(removed_footers),
130
+ )
131
+
132
+ with col3:
133
+ st.metric(
134
+ "Page numbers removed",
135
+ len(removed_page_numbers),
136
+ )
137
+
138
+ with st.expander("Show removed headers"):
139
+ if removed_headers:
140
+ st.dataframe(
141
+ removed_headers,
142
+ use_container_width=True,
143
+ )
144
+ else:
145
+ st.info("No headers were removed.")
146
+
147
+ with st.expander("Show removed footers"):
148
+ if removed_footers:
149
+ st.dataframe(
150
+ removed_footers,
151
+ use_container_width=True,
152
+ )
153
+ else:
154
+ st.info("No footers were removed.")
155
+
156
+ with st.expander("Show removed page numbers"):
157
+ if removed_page_numbers:
158
+ st.dataframe(
159
+ removed_page_numbers,
160
+ use_container_width=True,
161
+ )
162
+ else:
163
+ st.info("No page numbers were removed.")
164
+
165
+ st.divider()
166
+
167
+ st.subheader("Result per page")
168
+
169
+ st.dataframe(
170
+ result["page_results"],
171
+ use_container_width=True,
172
+ )
173
+
174
+ st.divider()
175
+
176
+ with st.expander("View text included in the count"):
177
+ st.text_area(
178
+ "Text",
179
+ result["included_text"],
180
+ height=400,
181
+ )
182
+
183
+ st.download_button(
184
+ label="Download text as TXT",
185
+ data=result["included_text"],
186
+ file_name="counted_text.txt",
187
+ mime="text/plain",
188
+ )
pdf_counter.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import re
2
  from collections import Counter
3
  import fitz
 
1
+ # pdf_counter.py
2
  import re
3
  from collections import Counter
4
  import fitz