Update app.py
Browse files
app.py
CHANGED
|
@@ -149,159 +149,6 @@ def split_pdf_by_pages(doc, output_dir=None):
|
|
| 149 |
|
| 150 |
return split_files, total_pages
|
| 151 |
|
| 152 |
-
#def merge_pdfs_in_groups(split_files, group_size):
|
| 153 |
-
# """
|
| 154 |
-
# split_files の PDF を group_size ごとにグループ化してマージします。
|
| 155 |
-
#
|
| 156 |
-
# Args:
|
| 157 |
-
# split_files (list): 分割された PDF ファイルのリスト(ファイルパスまたはバイトストリーム)。
|
| 158 |
-
# group_size (int): 1つのPDFにまとめるページ数。
|
| 159 |
-
#
|
| 160 |
-
# Returns:
|
| 161 |
-
# dict: グループ番号をキー、マージされたPDFデータを値とする辞書。
|
| 162 |
-
# """
|
| 163 |
-
# merged_files = {}
|
| 164 |
-
# try:
|
| 165 |
-
# # split_files を group_size ごとに分割
|
| 166 |
-
# for group_idx in range(0, len(split_files), group_size):
|
| 167 |
-
# # 新しいPDFドキュメントを作成
|
| 168 |
-
# new_doc = fitz.open()
|
| 169 |
-
#
|
| 170 |
-
# # 現在のグループのファイルを処理
|
| 171 |
-
# group_files = split_files[group_idx:group_idx + group_size]
|
| 172 |
-
# for file_index, file in enumerate(group_files):
|
| 173 |
-
# try:
|
| 174 |
-
# pdf_file = fitz.open(file) # PDFを開く
|
| 175 |
-
# except Exception as e:
|
| 176 |
-
# print(f"Error processing file {file}: {e}")
|
| 177 |
-
# continue
|
| 178 |
-
# else:
|
| 179 |
-
# new_doc.insert_pdf(pdf_file) # 新しいPDFに挿入
|
| 180 |
-
# pdf_file.close() # ファイルを閉じる
|
| 181 |
-
# del pdf_file
|
| 182 |
-
# gc.collect() # メモリ解放
|
| 183 |
-
# print(f"{file_index + 1} / {group_size} processed.") # 処理状況を表示
|
| 184 |
-
#
|
| 185 |
-
# # フォントの最適化
|
| 186 |
-
# new_doc.subset_fonts()
|
| 187 |
-
#
|
| 188 |
-
# # メモリ上に保存
|
| 189 |
-
# output_pdf = io.BytesIO()
|
| 190 |
-
# new_doc.save(
|
| 191 |
-
# output_pdf,
|
| 192 |
-
# deflate=True,
|
| 193 |
-
# garbage=4,
|
| 194 |
-
# deflate_fonts=True,
|
| 195 |
-
# use_objstms=1
|
| 196 |
-
# )
|
| 197 |
-
# output_pdf.seek(0)
|
| 198 |
-
#
|
| 199 |
-
# # マージされたPDFを辞書に保存
|
| 200 |
-
# merged_files[group_idx // group_size] = output_pdf
|
| 201 |
-
#
|
| 202 |
-
# print(f"Merged group {group_idx // group_size + 1} processed.") # 処理状況を表示
|
| 203 |
-
#
|
| 204 |
-
# except Exception as e:
|
| 205 |
-
# print(f"Error merging group {group_idx // group_size + 1}: {e}") # エラー表示
|
| 206 |
-
# raise
|
| 207 |
-
#
|
| 208 |
-
# return merged_files
|
| 209 |
-
|
| 210 |
-
#def merge_pdfs_in_groups(split_files, group_size):
|
| 211 |
-
# """
|
| 212 |
-
# split_files の PDF を group_size ごとにグループ化してマージします。
|
| 213 |
-
#
|
| 214 |
-
# Args:
|
| 215 |
-
# split_files (list): 分割された PDF ファイルのリスト(ファイルパスまたはバイトストリーム)。
|
| 216 |
-
# group_size (int): 1つのPDFにまとめるページ数。
|
| 217 |
-
#
|
| 218 |
-
# Returns:
|
| 219 |
-
# io.BytesIO: 最終的にマージされた1つのPDFデータ。
|
| 220 |
-
# """
|
| 221 |
-
# waiting = st.empty()
|
| 222 |
-
# counter = 0
|
| 223 |
-
# intermediate_files = [] # 中間生成されたPDFを一時保存するリスト
|
| 224 |
-
# try:
|
| 225 |
-
# # Step 1: split_files を group_size ごとに分割して中間PDFを作成
|
| 226 |
-
# for group_idx in range(0, len(split_files), group_size):
|
| 227 |
-
# # 新しいPDFドキュメントを作成
|
| 228 |
-
# new_doc = fitz.open()
|
| 229 |
-
# # 現在のグループのファイルを処理
|
| 230 |
-
# group_files = split_files[group_idx:group_idx + group_size]
|
| 231 |
-
# for file_index, file in enumerate(group_files):
|
| 232 |
-
# counter += 1
|
| 233 |
-
# try:
|
| 234 |
-
# pdf_file = fitz.open(file) # PDFを開く
|
| 235 |
-
# except Exception as e:
|
| 236 |
-
# print(f"Error processing file {file}: {e}")
|
| 237 |
-
# continue
|
| 238 |
-
# else:
|
| 239 |
-
# new_doc.insert_pdf(pdf_file) # 新しいPDFに挿入
|
| 240 |
-
# pdf_file.close() # ファイルを閉じる
|
| 241 |
-
# del pdf_file
|
| 242 |
-
# gc.collect() # メモリ解放
|
| 243 |
-
# print(f"{file_index + 1} / {group_size} processed.") # 処理状況を表示
|
| 244 |
-
#
|
| 245 |
-
# waiting.write(f"{counter} / {len(split_files)}")
|
| 246 |
-
#
|
| 247 |
-
# # フォントの最適化
|
| 248 |
-
# new_doc.subset_fonts()
|
| 249 |
-
#
|
| 250 |
-
# # メモリ上に保存
|
| 251 |
-
# output_pdf = io.BytesIO()
|
| 252 |
-
# new_doc.save(
|
| 253 |
-
# output_pdf,
|
| 254 |
-
# deflate=True,
|
| 255 |
-
# garbage=4,
|
| 256 |
-
# deflate_fonts=True,
|
| 257 |
-
# use_objstms=1
|
| 258 |
-
# )
|
| 259 |
-
# output_pdf.seek(0)
|
| 260 |
-
#
|
| 261 |
-
# # 中間PDFをリストに保存
|
| 262 |
-
# intermediate_files.append(output_pdf)
|
| 263 |
-
# print(f"Merged group {group_idx // group_size + 1} processed.") # 処理状況を表示
|
| 264 |
-
#
|
| 265 |
-
# # ドキュメントを閉じる
|
| 266 |
-
# new_doc.close()
|
| 267 |
-
#
|
| 268 |
-
# # Step 2: 中間PDFをすべて結合して最終的なPDFを作成
|
| 269 |
-
# final_doc = fitz.open()
|
| 270 |
-
# for i, intermediate_file in enumerate(intermediate_files):
|
| 271 |
-
# try:
|
| 272 |
-
# intermediate_pdf = fitz.open(stream=intermediate_file.getvalue(), filetype="pdf")
|
| 273 |
-
# final_doc.insert_pdf(intermediate_pdf)
|
| 274 |
-
# intermediate_pdf.close()
|
| 275 |
-
# print(f"Intermediate PDF {i + 1} merged into final document.")
|
| 276 |
-
# except Exception as e:
|
| 277 |
-
# print(f"Error merging intermediate PDF {i + 1}: {e}")
|
| 278 |
-
# continue
|
| 279 |
-
#
|
| 280 |
-
# final_doc.subset_fonts()
|
| 281 |
-
# # 最終PDFをメモリ上に保存
|
| 282 |
-
# final_output_pdf = io.BytesIO()
|
| 283 |
-
# final_doc.save(
|
| 284 |
-
# final_output_pdf,
|
| 285 |
-
# deflate=True,
|
| 286 |
-
# garbage=4,
|
| 287 |
-
# deflate_fonts=True,
|
| 288 |
-
# use_objstms=1
|
| 289 |
-
# )
|
| 290 |
-
# final_output_pdf.seek(0)
|
| 291 |
-
#
|
| 292 |
-
# # ドキュメントを閉じる
|
| 293 |
-
# final_doc.close()
|
| 294 |
-
# merged_files = {}
|
| 295 |
-
# merged_files[0] = final_output_pdf
|
| 296 |
-
# print("Final merged PDF created successfully.")
|
| 297 |
-
# #return final_output_pdf # io.BytesIO
|
| 298 |
-
# return merged_files
|
| 299 |
-
#
|
| 300 |
-
# except Exception as e:
|
| 301 |
-
# print(f"Error during merging process: {e}")
|
| 302 |
-
# raise
|
| 303 |
-
|
| 304 |
-
|
| 305 |
def merge_pdfs_in_groups(split_files, group_size):
|
| 306 |
"""
|
| 307 |
split_files の PDF を group_size ごとにグループ化してマージします。
|
|
|
|
| 149 |
|
| 150 |
return split_files, total_pages
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
def merge_pdfs_in_groups(split_files, group_size):
|
| 153 |
"""
|
| 154 |
split_files の PDF を group_size ごとにグループ化してマージします。
|