是否有任何脚本可以将文件夹图像转换为一个pdf

发布于 2024-11-16 11:09:25 字数 105 浏览 0 评论 0原文

我有很多文件夹,里面有很多图像。现在我想要每个文件夹一个 PDF,以便文件夹中包含的所有图像都变成 PDF。我有 1000 个文件夹,所以我想要一些可以批处理或可以进入文件夹并开始处理内容的东西。

I have many folders and inside that i have many images. Now i want one PDF per folder so that all images contained in folder goes into PDF. I have 1000s of folders so i want something which can batchprocess or which can walk in the folder and start processing things.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裂开嘴轻声笑有多痛 2024-11-23 11:09:25

我会用 ImageMagick 解决这个问题,而不是用 Python。 ImageMagick 有控制台工具“转换”。像这样使用它:

convert *.jpg foo.pdf

请参阅此处。 (取决于你使用的是Windows、Mac还是Linux,用Google应该很容易找到)

I'd solve this with ImageMagick, and not with Python. ImageMagick has the console tool 'convert'. Use it like this:

convert *.jpg foo.pdf

See here. (Depends on whether you use Windows, Mac or Linux, should be easy to find out with Google)

酒绊 2024-11-23 11:09:25

我用这段代码做了同样的事情。它使用Python(2.7而不是Python 3)和可从此处下载的reportlab包 http://www.reportlab .com/software/installation/ 并循环遍历您设置为“root”的所有子目录,并为每个文件夹中的所有 jpeg 创建一个 pdf。

import os
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader


root = "C:\\Users\\Harry\\" 

try:
   n = 0
   for dirpath, dirnames, filenames in os.walk(root):
       PdfOutputFileName = os.path.basename(dirpath) + ".pdf" 
      c = canvas.Canvas(PdfOutputFileName)
      if n > 0 :
           for filename in filenames:
                LowerCaseFileName = filename.lower()
                if LowerCaseFileName.endswith(".jpg"):
                     print(filename)
                     filepath    = os.path.join(dirpath, filename)
                     print(filepath)
                     im          = ImageReader(filepath)
                     imagesize   = im.getSize()
                     c.setPageSize(imagesize)
                     c.drawImage(filepath,0,0)
                     c.showPage()
                     c.save()
      n = n + 1
      print "PDF of Image directory created" + PdfOutputFileName

except:
     print "Failed creating PDF"

I used this code to do the same thing. It uses the Python (2.7 not Python 3)and the reportlab package downloadable from here http://www.reportlab.com/software/installation/ and loops thru all the subdirectories of what you set "root" to and creates one pdf of all the jpegs in each folder.

import os
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader


root = "C:\\Users\\Harry\\" 

try:
   n = 0
   for dirpath, dirnames, filenames in os.walk(root):
       PdfOutputFileName = os.path.basename(dirpath) + ".pdf" 
      c = canvas.Canvas(PdfOutputFileName)
      if n > 0 :
           for filename in filenames:
                LowerCaseFileName = filename.lower()
                if LowerCaseFileName.endswith(".jpg"):
                     print(filename)
                     filepath    = os.path.join(dirpath, filename)
                     print(filepath)
                     im          = ImageReader(filepath)
                     imagesize   = im.getSize()
                     c.setPageSize(imagesize)
                     c.drawImage(filepath,0,0)
                     c.showPage()
                     c.save()
      n = n + 1
      print "PDF of Image directory created" + PdfOutputFileName

except:
     print "Failed creating PDF"
嗫嚅 2024-11-23 11:09:25

我建议使用这样的方法在文档中运行 for 循环:

def __init__(self, location):
  if os.path.isdir(location): # search directory
    for infile in glob.glob(os.path.join(directory, '*.png')):
    print 'current file is: %s' % infile

在 for 循环中,我建议使用诸如 pyPDF< 之类的库/a>

I would suggest running for loops through your documents using something like this:

def __init__(self, location):
  if os.path.isdir(location): # search directory
    for infile in glob.glob(os.path.join(directory, '*.png')):
    print 'current file is: %s' % infile

Within the for loop I would suggest using a library such as pyPDF

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文