a3如何打印双面小册子_A3折叠成为小册子

a3如何打印双面小册子_A3折叠成为小册子程序功能:A3PDF_A4PDF问题:A3幅面的双面打印的在中间用骑马钉装订成小册子,拆开中间装订的订书钉由复印机一次性扫描成PDF文件。阅读不方便:页码错乱,A3幅面,左右两页。本程序将扫描的A3幅面的PDF文件转换成理顺页码单面的A4幅面的PDF文件。编程思路:PDF文件==>页面png(图片文件)(用到:pdf2image,oppler中的pdftommp.exe)==>图片文件A3大小切分成…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

程序功能: 用python将小册子打印扫描的A3幅面双页乱码的PDF文件转换A4幅面顺码的PDF文件
        问题:A3幅面的双面打印的在中间用骑马钉装订成小册子, 拆开中间装订的订书钉由复印机一次性扫描成PDF文件。
        阅读不方便:页码错乱,A3幅面,左右两页。
        本程序将扫描的A3幅面的PDF文件转换成理顺页码单面的A4幅面的PDF文件。
编程思路: PDF文件==>页面png(图片文件)(用到:pdf2image,poppler中的 pdftommp.exe )
               ==>图片文件A3大小切分成2个A4幅面的图片文件 (用到:PIL中的Image)
               ==>图片文件组成PDF文件(用到:img2pdf中的img2pdf.convert(pngList)

import os,sys
import img2pdf
'''
程序功能: 用python将小册子打印扫描的A3幅面双页乱码的PDF文件转换A4幅面顺码的PDF文件
        问题:A3幅面的双面打印的在中间用骑马钉装订成小册子, 拆开中间装订的订书钉由复印机一次性扫描成PDF文件。
        阅读不方便:页码错乱,A3幅面,左右两页。
        本程序将扫描的A3幅面的PDF文件转换成理顺页码单面的A4幅面的PDF文件。
编程思路: PDF文件==>页面png(图片文件)(用到:pdf2image,oppler中的 pdftommp.exe )
               ==>图片文件A3大小切分成2个A4幅面的图片文件 (用到:PIL中的Image)
               ==>图片文件组成PDF文件(用到:img2pdf中的img2pdf.convert(pngList)

附注:实测中使用png格式的形成的A4幅面的PDF较小。

重要事项:
(1) 程序安装在d:盘(或e:)的d:\leader
(2) 解压poppler-0.68.0_x86后得到的bin目录下的文件安装到 d:\leader\bin;
    并将d:\leader\bin加入到windows的path变量中。(下面的批处理文件已经解决这个问题)
(3) 批处理文件可以如下:
    rem main.cmd
    path d:\leader\bin;%path%
    d:
    cd \leader
    python main.prg %1

(4) 运行时,可以: main d:\A3pdf目录
    也可以: 直接main运行。不添加命令行参数使用缺省A3PDF目录为: d:\leader\pdf
    使用时只需将要转换的A3pdf文件copy到 d:\leader\pdf之下即可。
    转换完成的文件在 d:\leader\pdf\A4子目录下。
    中间转换时产生的图片文件在 d:\leader\pdf\PNG 子目录下。这些图片文件可以删除。

版本 0.1
    版本 01,使用全局变量,各个函数均在一个文件中。

程序使用了几个库:
pip install pillow
pip install PyPdf3
pip install pdf2image
pip install img2pdf

程序还使用了 poppler-0.68.0_x86
pdf2image是包装器,poppler是转换过程真正需要的。


编程 叶照清 363992124@qq.com
日期 2021.01.25

=============
Poppler for Windows
I have been using the Poppler library for some time, over a series of various projects. It’s an open source set of libraries and command line tools, very useful for dealing with PDF files. Poppler is targeted primarily for the Linux environment, but the developers have included Windows support as well in the source code. Getting the executables (exe) and/or dlls for the latest version however is very difficult on Windows. So after years of pain, I jumped on oDesk and contracted Ilya Kitaev, to both compile with Microsoft Visual Studio, and also prepare automated tools for easy compiling in the future. Update: MSVC isn’t very well supported, these days the download is based off MinGW.

So now, you can run the following utilities from Windows!

PDFToText – Extract all the text from PDF document. I suggest you use the -Layout option for getting the content in the right order.
PDFToHTML – Which I use with the -xml option to get an XML file listing all of the text segments’ text, position and size, very handy for processing in C#
PDFToCairo – For exporting to images types, including SVG!
Many more smaller utilities
Download

Latest binary : poppler-0.68.0_x86
http://blog.alivate.com.au/wp-content/uploads/2018/10/poppler-0.68.0_x86.7z
'''

from pdf2image.exceptions import (
    PDFInfoNotInstalledError,
    PDFPageCountError,
    PDFSyntaxError
)
from pdf2image import convert_from_path
import os,sys,PyPDF3
from PIL import Image

def pdf2img(pdf_file):
    basename=os.path.basename(pdf_file)[:-4]
    try:
        images = convert_from_path(pdf_file)
        for idx, img in enumerate(images):
            path=path_png+rf'\{basename}_{idx:02d}.png'
            img.save(path)
    except Exception as e:
        print(e)

def pic_half(filename1,No,MaxPage):
    basename=os.path.basename(filename1)[:-3]
    basefileName= path_png+'\\'+basename
    MaxPage+=1
    
    img = Image.open(filename1+'.png')
    size = img.size
    #print(size)

    # 准备将图片切割成2张小图片
    weight = int(size[0] // 2)
    height = int(size[1] // 1)
    # 切割后的小图的宽度和高度
    #print(weight, height)
    for j in range(1):
        for i in range(2):
            box = (weight * i, height * j, weight * (i + 1), height * (j + 1))
            #print(box)
            imgHalf = img.crop(box)
            if No%2==1:
                if i==0:
                    fsave= basefileName+f'_A4_{(MaxPage-No):02d}.png'
                else:
                    fsave= basefileName+f'_A4_{No:02d}.png'
            else:
                if i==1:
                    fsave= basefileName+f'_A4_{(MaxPage-No):02d}.png'
                else:
                    fsave= basefileName+f'_A4_{No:02d}.png'
                
##            print('\t'+fsave)
            imgHalf.save(fsave)
    img.close()

def one_pdf(pdf_file1):
##    '''
##        A3.pdf 总页数 测试的是10页==》对折20页
##    '''
##    try:
##        pdf_stream = open(pdf_file1,'rb')
##        pdf = PyPDF3.PdfFileReader(pdf_stream)
##    except:
##        print(f"{pdf_file1} 不是合法的PDF文件!")
##        exit(1)
##             
##    maxP=pdf.numPages
##    pdf_stream.close()
##    del pdf
    
    pdf2img(pdf_file1)
        
    basename=os.path.basename(pdf_file1)[:-4]
    
    for i in range(1,maxP+1):
        A3_png=path_png+f'\\{basename}_{i-1:02d}'
        print(A3_png)
        No=i
        pic_half(A3_png,No,maxP*2)

def doImg2Pdf(fileName):
    bb=pdf_b_name[:-4]
    with open(f"{A4_dir}\\{bb}_A4.pdf", "wb") as f:
        
        #fileList = os.listdir(fileName)
        #print(fileList)
        pngList = []
        for ii in range(1,maxP*2+1):
           
            pngName =f'{bb}_A4_{ii:02d}.png'
            print('\t'+pngName)
            pngList.append(pngName) 
        pfn_bytes = img2pdf.convert(pngList)
        f.write(pfn_bytes)
    print(f"{A4_dir}\\{bb}_A4.pdf 转换完成。\n")


################################       
root = os.path.abspath(os.path.dirname(__file__))
dd=''
if root.find(":") == 1:
    print(__file__)
    dd=root[:2]
path =dd+r'\LEADER\PDF'
path_a4 = path+r'\A4'
path_png = path+r'\PNG'
PDF_list=[]
maxP=10

if len(sys.argv)>1:
    path = sys.argv[1]
    
if os.path.isdir(path):
    path_a4 = path+'\\A4'
    path_png = path+'\\PNG'
    if not os.path.exists(path_a4) : os.makedirs(path_a4)
    if not os.path.exists(path_png): os.makedirs(path_png)                
    Dir_l = os.listdir(path)
    for ff in Dir_l[:]:
        if ff.find('.pdf') == -1:
            Dir_l.remove(ff)
    print("需转换的文件列表:")
    for i in range(len(Dir_l)):
        print(f'{i:04d}\t{Dir_l[i]}')
    PDF_list = Dir_l
    print(f"\n需转换的文件总数:{i+1:04d}")
else:
    print( f'{path} :非法目录')
    exit(1)

if len(PDF_list) == 0 : print("无PDF文件!");exit(0)



#Main_loop
for pdf_file in PDF_list:
    full_fileName = path+'\\'+pdf_file
    print(f'{full_fileName} 转换中。。。')

    try:
        pdf_stream = open(full_fileName,'rb')
        pdf = PyPDF3.PdfFileReader(pdf_stream)
    except:
        print(f"{Full_fileName} 不是合法的PDF文件!")
        exit(1)
             
    maxP=pdf.numPages
    pdf_stream.close()
    del pdf
    
    one_pdf(full_fileName)

    pdf_file = full_fileName   #"e:\TEST\叶照清.pdf"
    png_dir = os.path.dirname(pdf_file)+'\\PNG'
    A4_dir  = os.path.dirname(pdf_file)+'\\A4'
    pdf_b_name = os.path.basename(pdf_file)

    os.chdir(png_dir)

    doImg2Pdf(png_dir)

#eof

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/196817.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • goland激活_最新在线免费激活

    (goland激活)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html08G05E7DZH-eyJsaWNlbnNlSW…

    2022年3月28日
    69
  • 查看g++/gcc版本

    查看g++/gcc版本windows查看gcc/g++版本cmd命令行gcc–versiong++–version

    2022年6月26日
    181
  • LARS(最小角回归)

    LARS(最小角回归)优缺点LARS是一个适用于高维数据的回归算法。优点: 特别适合于特征维度n远高于样本数m的情况。 算法的最坏计算复杂度和最小二乘法类似,但是其计算速度几乎和前向选择算法一样 可以产生分段线性结果的完整路径,这在模型的交叉验证中极为有用 缺点:由于LARS的迭代方向是根据目标的残差而定,所以该算法对样本的噪声极为敏感。…

    2022年4月20日
    36
  • 服务器查询外网ip的方法

    服务器查询外网ip的方法curlhttpbin.org/ip[root@yx01~]#curlhttpbin.org/ip{“origin”:”120.24.109.9″}[root@yx01~]#

    2022年5月1日
    51
  • Java HashSet的实现原理详解

    Java HashSet的实现原理详解HashSet是JavaMap类型的集合类中最常使用的,本文基于Java1.8,对于HashSet的实现原理做一下详细讲解。(Java1.8源码:http://docs.oracle.com/javase/8/docs/api/)一、HashSet实现原理总结HashSet的实现原理总结如下:①是基于HashMap实现的,默认构造函

    2025年7月3日
    0
  • 在线编辑、在线预览、在线转换,基于wps.js + java + react / vue,无需任何插件,零安装

    基于wps.js在线编辑、在线预览、在线转换前台vue/react后台java,无需任何插件,零安装,可以直接配置完打包部署,也可直接集成项目中,或者二次开发,后续会不定期更新,有好的建议或者意见请留言。请下载demo的时候给个star,谢谢!亮点………示例地址https://ljserver.cn/wpsonline前台vuedemo下载地址点击此处下载vue-demo后台javademo下载地址点击此处下载java-demo…

    2022年4月12日
    382

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号