2023年6月21日 星期三

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

 系列文章:
3. 只要點兩下,就能將一堆的Doc與Docx 轉成 PDF
3.https://skjhcreator.blogspot.com/2023/05/docdocx-pdf.html
4.https://skjhcreator.blogspot.com/2023/06/jpgpdfjpgpdf.html
5.只要點兩下,就能將放進input的一堆PDF轉成各自的WORD
5.https://skjhcreator.blogspot.com/2023/10/inputpdfword.html
6.只要點兩下,就能將放進input的一堆PDF轉成在ouput資料夾內的各自的WORD 
6.https://skjhcreator.blogspot.com/2023/10/inputpdfouputword.html

7.只要點兩下,就能夠將InputAndOutput資料夾底下的子子孫孫資料夾內所有Word通通轉成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.




只要點兩下,就可以將資料夾input內的所有Word通通轉成一個PDF

系列文章: 1. python 不管何時何地,只要點兩下,資料夾內的所有pdf都會合併成一個pdf https://skjhcreator.blogspot.com/2022/06/pythonpdfpdf.html 2. python 只要點兩下,分別對各資料夾內的pdf合併,...