I want to put together an image file (jpg or JPEG or JPG) of the details imported by a scanner etc. into a PDF file Details are divided by directory, and each division is put together in a PDF file. In the directory, they are organized by date
ls ~/scan/meisai/
Car insurance statement
Car insurance statement/2017/
Car insurance statement/2018/
Car insurance statement/2019/
Car insurance statement/2020/
Credit card A/2017/
Credit card A/2018/
Credit card A/2019/
Credit card A/2020/
Medical statement 2020_01_20.jpg
.DS_store
There are multiple image files in such a directory structure. Summarize it as follows
ls ~/scan/meisai_pdf/
Car insurance statement.pdf
Credit card A.pdf
.DS_store
Manually delete the used image file.
python3 import os import img2pdf
import os
import img2pdf
EXP_DIR = "/Users/myname/scan/meisai"
OUT_DIR = "/Users/myname/scan/meisai_pdf"
def extract_image_file_list(folder_path):
ret = []
if os.path.isdir(folder_path) == False:
return ret
for t in sorted(os.listdir(path=folder_path)):
if os.path.isdir(folder_path + "/" + t):
#Follow the directory recursively
ret += extract_image_file_list(folder_path + "/" + t)
else:
ret.append(folder_path + "/" + t)
return ret
def image_to_pdf(pdf_name,image_list):
convert_target = []
for i in image_list:
if i.endswith(".jpg ") or i.endswith(".JPG") or i.endswith(".jpeg "):
convert_target.append(i)
if len(convert_target) == 0:
return
with open(pdf_name + ".pdf", "wb") as f:
f.write(img2pdf.convert(convert_target))
#os.chdir(EXP_DIR) #If you want to go to a directory and process with a relative path.
for path in os.listdir(path=EXP_DIR):
print(path)
files = extract_image_file_list(EXP_DIR + "/" + path)
image_to_pdf(OUT_DIR + "/" + path,files)
Recommended Posts