[PYTHON] Convert multiple jpg files to one PDF file

Thing you want to do

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.

What was used

python3 import os import img2pdf

code

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

Convert multiple jpg files to one PDF file
Convert PDF files to PNG files with GIMP
Combine multiple python files into one python file
[Python & Unix] Combine multiple PDF files into one.
Batch convert PSD files in directory to PDF
Beginners try to convert Word files to PDF at once
Convert HTML to text file
Convert genbank file to gff file
Batch convert image files uploaded to MS Forms / Google Forms to PDF
Convert PDF to Documents by OCR
Convert A4 PDF to A3 every 2 pages
How to convert 0.5 to 1056964608 in one shot
Convert from pdf to txt 2 [pyocr]
Convert PDF to image with ImageMagick
Convert files written in python etc. to pdf with syntax highlighting
Convert a large number of PDF files to text files using pdfminer
[Python] How to convert db file to csv
How to convert Python to an exe file
Convert matplotlib graphs to emf file format
Convert from PDF to CSV with pdfplumber
Convert psd file to png in Python
Convert PDF attached to email to text format
Convert HEIC files to PNG files with Python
Convert multiple CSVs to each Excel sheet
[Bash] Redirection of multiple lines to multiple files
Create an API to convert PDF files to TIF images with FastAPI and Docker
How to convert Web pages to PDF, PNG, JPG with VBA (Excel) (Selenium Basic)
Convert FBX files to ASCII <-> BINARY in Python
Convert PDF to image (JPEG / PNG) with Python
Convert svg file to png / ico with Python
Convert UTF-8 CSV files to read in Excel
Convert a multidimensional list (array) to one dimension
[Small story] Easy way to convert Jupyter to PDF
Convert multiple proto files at once with python
Sample to put Python Kivy in one file
Convert to HSV
A script that combines multiple pages of a PDF file into one page without margins
Combine multiple csv files into one csv file with python (assuming only one line of header)
Convert jupyter notebook .ipynb files to python executable .py files
Convert a text file with hexadecimal values to a binary file
[Python] Convert PDF text to CSV page by page (2/24 postscript)
Convert the image in .zip to PDF with Python
How to convert JSON file to CSV file with Python Pandas
Convert vector of integer values to one hot representation
How to convert a mel spectrogram back to a wav file
How to add page numbers to PDF files (in Python)
Convert multiple Jupyter notebooks together into an html file
How to convert Json file to CSV format or EXCEL format
Convert translation resource files (.po) to XLIFF format (.xlf)
Convert MOL files to SMILES via standard I / O
Combine multiple Excel files loaded using pandas into one
Convert Pascal VOC format xml file to COCO format json file