我的python 3.6代码在IDLE和PyCharm IDE上完美运行,但是当我尝试编译为exe时出现pyinstaller错误并且exe将无法运行。
我导入了几个外部python模块来完成工作。
我错过了什么吗?
#!/usr/bin/env python3
import io
import os
import shutil
import subprocess
import time
import PyPDF2
import easygui
import numpy as np
import pandas as pd
from PyPDF2 import PdfFileReader, PdfFileWriter
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from selenium import webdriver
# User to input file
userpath = easygui.fileopenbox()
print('Loading request file:')
print(userpath)
print('\n')
# Read excel file document with 4 columns: Document, Batch Number, Prints, Duplex
documentlist = pd.read_excel(userpath, usecols='A,B,C,D')
df = pd.DataFrame(documentlist)
# Format column Document numbers to 3 digits
df['Document'] = df['Document'].apply(lambda x: '{0:0>3}'.format(x))
# Deactivate Pandas silly message
pd.options.mode.chained_assignment = None
# Convert numeric values to string type FAR-BR-02.XXX
value = 'FAR'
df['Document'] = df['Document'].astype(str)
list = df['Document']
for x in range(len(list)):
if value not in list[x]:
list[x] = 'FAR-BR-02.' + str(list[x])
else:
continue
df['Document'] = list
print(df)
downloadlist = np.unique(list)
print('\n')
print('Your request contains', len(documentlist['Document']), 'documents, of which, ')
print(len(downloadlist), ' are unique (no repeats) to be retrieved from Proquis: ')
print('\n')
print(downloadlist)
print('\n')
# Prep a temp folder for downloads
# Check if the temp folder exists otherwise, create it
temppath = 'C:/pdfstampertemp/'
if not os.path.exists(temppath):
os.makedirs(temppath)
# Check temp folder is empty otherwise, clean it
if os.listdir(temppath) != []:
fileList = os.listdir(temppath)
for fileName in fileList:
os.remove(temppath + fileName)
# Download files from SSL Proquis using Chromedriver
print('Downloading files from Proquis. Authenticate if required.')
chrome_options = webdriver.ChromeOptions()
preferences = {"download.default_directory": 'C:\pdfstampertemp',
"directory_upgrade": True,
"download.prompt_for_download": False,
"safebrowsing.enabled": True}
chrome_options.add_experimental_option("prefs", preferences)
driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path=r'chromedriver.exe')
url = 'https://proquis.btgplc.com/viewdocument.aspx?DOCNO='
urllist = downloadlist
for x in range(len(downloadlist)):
urllist[x] = url + str(downloadlist[x])
driver.get(urllist[x])
print('Download completed!')
print ('\n')
time.sleep(1)
driver.quit()
# Decrypt downloaded PDFs
# Copy qpdf to temp directory
print('Preparing PDFs ...')
qpdffiles = ['qpdf.exe','qpdf21.dll','libwinpthread-1.dll','libstdc++-6.dll','libiconv2.dll','libgcc_s_dw2-1.dll']
for x in range(len(qpdffiles)):
shutil.copy2(qpdffiles[x], 'c://pdfstampertemp/' + qpdffiles[x])
# Change cwd to decrypt
installdir = os.getcwd()
os.chdir('c://pdfstampertemp/')
# Call qpdf to decrypt pdfs
workinglist = np.unique(list)
downloadlist = np.unique(list)
for x in range(len(downloadlist)):
downloadlist[x] = downloadlist[x] + '.PDF'
workinglist[x] = 'D' + downloadlist[x]
subprocess.run(["qpdf.exe", "--decrypt", downloadlist[x], workinglist[x]])
print('Ready! Stamping now. This may take some time according with the PDF size. Please wait...')
print('\n')
# Set the cwd back to the install directory
os.chdir(installdir)
# Order the file list by batch number
df = df.sort_values('BatchNo')
# Prep: declare variables and create first stamp headers
inputpdf = 'D' + df['Document'] + '.PDF'
outputpdf = 'SD' + df['Document'] + '.PDF'
batchno = df['BatchNo']
copies = df['Copies']
duplex = df['Single/Double']
stamp = ['StampP.pdf', 'StampL.pdf']
# Bach number position in page Portrait and Landscape (x,y)
xp = 400
yp = 780
xl = 640
yl = 530
# Create inital stapms for first entry
batch = str(batchno[1])
# Portrait: create a new PDF with Reportlab, insert the text in set location with specified font
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=A4)
can.setFont('Times-Bold', 16)
can.drawString(xp, yp, batch)
can.save()
# move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read the existing PDF
existing_pdf = PdfFileReader(open("BlankStampP.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open("StampP.pdf", "wb")
output.write(outputStream)
outputStream.close()
# Landscape: create a new PDF with Reportlab, insert the text in set location with specified font
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=A4)
can.setFont('Times-Bold', 16)
can.drawString(xl, yl, batch)
can.save()
# move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read the existing PDF
existing_pdf = PdfFileReader(open("BlankStampL.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open("StampL.pdf", "wb")
output.write(outputStream)
outputStream.close()
# Watermark the first PDF
input = temppath + inputpdf[0]
inputfile = open(input, 'rb')
pdfReader = PyPDF2.PdfFileReader(inputfile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
inputfilePage = pdfReader.getPage(pageNum)
page = pdfReader.getPage(pageNum).mediaBox
if (page.getUpperRight_x() - page.getUpperLeft_x()) > (page.getUpperRight_y() - page.getLowerRight_y()):
stamp ='StampL.pdf'
else:
stamp = 'StampP.pdf'
pdfWatermarkReader = PyPDF2.PdfFileReader(open(stamp, 'rb'))
inputfilePage.mergePage(pdfWatermarkReader.getPage(0))
pdfWriter.addPage(inputfilePage)
resultPdfFile = open(str(temppath + '0' + outputpdf[0]), 'wb')
pdfWriter.write(resultPdfFile)
inputfile.close()
resultPdfFile.close()
# Rest of the files - create new stamp only if needed
for i in range(1,len(inputpdf)):
if batchno[i] != batchno[i-1]:
# Portrait: create a new PDF with Reportlab, insert the text in set location with specified font
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=A4)
can.setFont('Times-Bold', 16)
can.drawString(xp, yp, str(batchno[i]))
can.save()
# move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read the existing PDF
existing_pdf = PdfFileReader(open("BlankStampP.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open("StampP.pdf", "wb")
output.write(outputStream)
outputStream.close()
# Landscape: create a new PDF with Reportlab, insert the text in set location with specified font
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=A4)
can.setFont('Times-Bold', 16)
can.drawString(xl, yl, str(batchno[i]))
can.save()
# move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read the existing PDF
existing_pdf = PdfFileReader(open("BlankStampL.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open("StampL.pdf", "wb")
output.write(outputStream)
outputStream.close()
# Stamp them
input = temppath + inputpdf[i]
inputfile = open(input, 'rb')
pdfReader = PyPDF2.PdfFileReader(inputfile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
inputfilePage = pdfReader.getPage(pageNum)
page = pdfReader.getPage(pageNum).mediaBox
if (page.getUpperRight_x() - page.getUpperLeft_x()) > (page.getUpperRight_y() - page.getLowerRight_y()):
stamp = 'StampL.pdf'
else:
stamp = 'StampP.pdf'
pdfWatermarkReader = PyPDF2.PdfFileReader(open(stamp, 'rb'))
inputfilePage.mergePage(pdfWatermarkReader.getPage(0))
pdfWriter.addPage(inputfilePage)
resultPdfFile = open(str(temppath + str(i) + outputpdf[i]), 'wb')
pdfWriter.write(resultPdfFile)
inputfile.close()
resultPdfFile.close()
print("Stamping has completed.")
print('Script terminated. Have a nice day!')
我想我需要以某种方式定义这是主要模块,其他是导入。
有任何想法吗?
这是由于Python 3.7的系统路径未在Windows 10中错误设置。
现在全部完成,我能够使用pyinstaller创建一个EXE(虽然显示了多个警告和错误)。
EXE的运行方式与Python 3.7中的py完全相同。