2023年6月21日 星期三

只要點兩下,就能將一堆的JPG轉成一個PDF,並以JPG所在的資料夾名稱為PDF的檔名

        Just two clicks and you can convert a bunch of JPGs into a single PDF, with the PDF file named after the folder where the JPGs are located. 

       現在遇到一個問題,這個問題是要將一堆的JPG轉成一個PDF,並且這個PDF的檔名為一堆JPG所在的資料夾名稱。
       I am currently facing an issue where I need to convert a bunch of JPGs into a single PDF, and the PDF file should be named after the folder where the JPGs are located.
 
       在資料來源1.Convert Images to PDF using Python,發現程式碼如下:
       In data source 1, "Convert Images to PDF using Python," I found the following code:
from PIL import Image

image_1 = Image.open(r'path where the image is stored\file name.png')
im_1 = image_1.convert('RGB')
im_1.save(r'path where the pdf will be stored\new file name.pdf')

所以我需要安裝套件 Pillow、jpg檔案位置。
So I need to install the Pillow package and provide the location of the JPG files. 
                     下載檔案解壓密碼:demo1234
Here is the website where you can download the program and find instructions:
Download。Extraction Password: demo1234
使用教學(Instructional videos):

以下是開發過程與原始碼 (Development process and code):
安裝Pillow套件(Install the Pillow suite)      
安裝套件指令(command):pip install Pillow
 
       程式名稱(Program name):jpg2pdf.py
       程式內容(Code):
from PIL import Image
import os

#取得當前目錄
Path = os.getcwd()
#取得input路徑
InputPath = Path+'\\input\\'
#取得input資料夾下的目錄或檔案
dirs = os.listdir(InputPath)
# 建立字典與串列
pdfname = dict()
pdfadds = []
jpglist = []
#逐一檢查input資料夾下的目錄或檔案
for dir in dirs:
    #print(dir)
    #目錄的處理
    if os.path.isdir(InputPath+'\\'+dir+'\\'):
        pdfadds = os.listdir(InputPath+'\\'+dir)
        pdfname[dir] = pdfadds
    #檔案的處理    
    elif os.path.isfile(InputPath+'\\'+dir):
        #檢查檔案副檔名為jpg
        if dir.split('.')[1] == 'jpg':
            jpglist.append(dir)
# 目錄-> jpglist        
pdfname['input'] = jpglist            

#jpg 轉 pdf
for key in pdfname.keys():
    TempIm_list = []
    Tempk = 0
    TempPdfName = str(key+'.pdf')
    for i in pdfname[key]:
        if key != 'input':
            # 處理放在資料夾內的jpg
            ImagePath = InputPath+'\\'+key+'\\'+i
        elif key == 'input':
            # 處理放在input 的jpg
            ImagePath = InputPath+'\\'+i
        Tempk = Tempk + 1
        if Tempk == 1:
            TempImage_1 = Image.open(ImagePath)
            TempIm_1 = TempImage_1.convert('RGB')
        else:
            TempImage = Image.open(ImagePath)
            TempIm= TempImage.convert('RGB')
            TempIm_list.append(TempIm)
    TempIm_1.save(TempPdfName,save_all=True,append_images=TempIm_list)

       
資料來源:
4.




Laravel 12 Model 資料庫中的資料表,並提供與資料庫互動的介面

相關系列文章: 1. 在 windows 10 安裝 laravel 12 studentManagement環境與設定 2. laravel 12 route 路由 3. laravel 12 Blade Templates 網頁模版 4. laravel 12 Control...