python批量生成pdf文件工具

废话少说,我们直接上代码:

#coding=utf-8
'''实现python生成pdf文件功能
1.判断当前目录是否存在pdfPath文件夹目录,如果不存在就创建
2.在文件夹中创建pdf文件,使用for循环创建
3.名字TEST0001-TEST1000名字依次往后迭代
'''
from reportlab.pdfgen import canvas
import os,sys

def judgePath():
    '''
    判断当前目录是否存在pdfData文件夹目录,如果不存在就创建
    :return:
    '''
    # 当前目录和pdfpath目录拼接路径
    pdf_path = os.path.join(os.getcwd(),r'pdfData')
    print(pdf_path)
    if not os.path.exists(pdf_path):  # 如果目录pdf_path不存在
        os.mkdir(pdf_path)#就创建

    # 指向pdf文件路径
    test_path = os.path.join(pdf_path,'TEST%s.PDF')
    print("本工具支持批量生成pdf文件")
    print("-------©版权所有人 周二狗")
    print("请输入文件数量")
    aInt = input('>:')
    print(type(aInt))
    print(type(eval(aInt)))
    if aInt.isdigit():
        for i in range(eval(aInt)):
            print("开始生成第%s个文件" %(i+1))
            if not os.path.exists(test_path % (i+1)): # 判断文件是否存在,不存在,就执行
                CanvasPdf(test_path % (i+1))
        print("文件生成成功!")
        os.system("pause")
    else:
        print("请输入正整数")

def CanvasPdf(test_path):
    '''
    创建一个pdf文件
    :param test_path: pdf文件的路径
    :return:
    '''
    c=canvas.Canvas(test_path)
    c.drawString(30,800,"All is still and the wind is calm")
    c.showPage()
    c.save()
    return c

def main():
    judgePath()

if __name__ == '__main__':
    main()

有兴趣的小伙伴可以使用pyInstaller模块将源码打包成.exe可执行程序,这样在没有python解释器的环境下依然可以使用。
ps:语言只是工具,重要的我们想做什么!这才是兴趣所在。

猜你喜欢

转载自blog.csdn.net/qq_42833469/article/details/86692840
今日推荐