简体   繁体   English

在 Python 中将图像转换为 PDF

[英]convert Image to PDF in Python

I want to convert image to pdf in python.我想在 python 中将图像转换为 pdf。

this is my code:这是我的代码:

import docx
from docx.shared import Inches, Mm
import os
from PIL import Image
from PIL import Image, ImageDraw, ImageFont
from docx2pdf import convert
from wand.image import Image as Im

image_dir = os.listdir(os.getcwd()+'\\Images')
print(len(image_dir))
doc = docx.Document()
section = doc.sections[0]
section.page_height = Mm(1000)
section.page_width = Mm(580)
section.left_margin = Mm(25.4)
section.right_margin = Mm(25.4)
section.top_margin = Mm(25.4)
section.bottom_margin = Mm(25.4)
section.header_distance = Mm(12.7)
section.footer_distance = Mm(12.7)
p = doc.add_paragraph()
x = 0
for i in range(0, len(image_dir)):
    size = (130, 160)
    temp_img = Image.open(os.getcwd()+'\\Images\\'+image_dir[i])
    temp_img = temp_img.resize(size)
    # temp_img.thumbnail(size, Image.ANTIALIAS)
    
    # temp_img.show()
    background = Image.new('RGBA', (500, 220), (255, 255, 255, 0))
    for k in range(0, 3):
        background.paste(temp_img, (0,0))
        background.paste(temp_img, (150,0))
        background.paste(temp_img, (300,0))
    font = ImageFont.truetype(r'arial.ttf', 25) 
    d1 = ImageDraw.Draw(background)
    d1.text((5, 160), image_dir[i][:-4], fill =(0, 0, 0), font = font)
    background.save("temp.png")
    with Im(filename ="temp.png") as img:
    
        # generating sharp image using sharpen() function.
        img.sharpen(radius = 16, sigma = 8)
        img.save(filename ="temp1.png")
    r = p.add_run()
    r.add_picture("temp1.png")
        
doc.save('demo1.docx')
convert("demo1.docx")

This code run well.这段代码运行良好。 But IMG quality of pdf is poor and process is very slowly.但是pdf的IMG质量很差,处理速度很慢。

I want to improve convert speed.我想提高转换速度。

Somebody help me.来人帮帮我。 Thank you.谢谢你。

I think PIL is enough for you to combine an image with text into a PDF.我认为PIL足以让您将图像与文本组合成 PDF。

for example, you can save the combined images with text like this例如,您可以使用这样的文本保存组合图像

import os
from PIL import Image, ImageDraw, ImageFont

image_dir = os.listdir(os.getcwd()+'\\Images')
for i in range(0, len(image_dir)):
    size = (130, 160)
    temp_img = Image.open(os.getcwd()+'\\Images\\'+image_dir[i])
    temp_img = temp_img.resize(size)

    background = Image.new('RGB', (500, 220), (255, 255, 255))
    for k in range(0, 3):
        background.paste(temp_img, (0,0))
        background.paste(temp_img, (150,0))
        background.paste(temp_img, (300,0))
    font = ImageFont.truetype('arial.ttf', 25) 
    d1 = ImageDraw.Draw(background)
    d1.text((5, 160), image_dir[i][:-4], fill =(0, 0, 0), font = font)
    background.save(f"{image_dir[i][:-4]}.pdf")

This is my try.这是我的尝试。 Just give the directory path and its done;只需给出目录路径并完成; you end up with a subdirectory containing all the PDF files你最终会得到一个包含所有 PDF 文件的子目录

from pathlib import *
from PIL import Image

# path input #
path = input("Enter The path of you Images directory: \n")
path = Path(f"{path}")
####################################################
# making a subdirectory to contain the PDF version #
(path/"PDF's").mkdir(exist_ok=True) 

# iterating over every file in the given directory #
# we use try function to ignore non image files #
for pp in path.glob("*"):
    try: 
        with Image.open(pp) as image:
            im = image.convert("RGB")
            pth = Path(f"{path}\PDF's\{pp.stem}.pdf")
            im.save(pth, save_all=True)
    except:
        pass

print("Conversion Completed ...")

# if you want to merge PDF's uncomment the next segment 

#import PyPDF2


# merger = PyPDF2.PdfFileMerger()

# path = Path(f"{path}\PDF's")
# # (path/"MergedPDF's").touch()
# if (path/"MergedPDF's.pdf").exists():
#     (path/"MergedPDF's.pdf").unlink()


# for pdf in path.glob("*.pdf"):
#     merger.append(PyPDF2.PdfReader(pdf, "rb"))

# output = open(path/"MergedPDF's.pdf", "wb")
# merger.write(output)
# merger.close()

Pillow solution is pretty good in my opinion.我认为枕头解决方案非常好。 But if u need more control over your pdf I suggest u to use PyMuPDF.但是如果你需要更多地控制你的 pdf 我建议你使用 PyMuPDF。 Best library for pdf manipulation, hands down. pdf 操纵的最佳库,毫无疑问。

# copy image files to PDF pages
# each page will have image dimensions
import fitz

doc = fitz.open()                           # new PDF
imglist = [ ... image file names ...]       # e.g. a directory listing
for img in imglist:
        imgdoc=fitz.open(img)               # open image as a document
        pdfbytes=imgdoc.convert_to_pdf()    # make a 1-page PDF of it
        imgpdf=fitz.open("pdf", pdfbytes)
        doc.insert_pdf(imgpdf)              # insert the image PDF
doc.save("allmyimages.pdf")

Is pretty handy if u want to add metadata:如果您想添加元数据,这非常方便:

import fitz

doc = fitz.open()
metadata = {'producer': 'YourName', 'format': 'PDF 1.4', 'encryption': None, 'author': 'YourName',
'modDate': 'none', 'keywords': 'none', 'title': 'YourPdf', 'creationDate': 'none',
'creator': 'none', 'subject': 'none'} # your metadata needs to be a dictionary
doc.set_metadata(metadata)

table of contents:目录:

import fitz
doc = fitz.open()

# the toc is a basically a list of lists. Each list has 3 elements:
# - first: the layer of the toc link (basically the main category (1), subcategory (2) etc..)
# - second: title of the layer
# - third: page where the title is linked
table_of_content = [[1, 'The PyMuPDF Documentation', 1], [2, 'Introduction', 1], [3, 'Note on the Name fitz', 1], [3, 'License', 1]]
doc.set_toc(table_of_content)

etc... I think that giving a look at the documentation is pretty useful等等...我认为看一下文档非常有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM