求助各位大神:!!!PY打包EXE文件后,运行总是报错,抓狂ing

小白求人写了一段代码后,发现在自己电脑上PY文件可以运行,打包EXE后也可以生成.exe文件,但是一运行就报错,求解。。。

(在写代码的人电脑上可以打包,他也不知道为啥)

from easygui import msgbox

import pandas as pd

from tkinter import filedialog

from datetime import datetime

import code128

import warnings

from qrcode import QRCode, constants

from io import BytesIO

from PIL import Image, ImageDraw, ImageFont

from add_float_picture import add_float_picture

from docx import Document

from docx.oxml.ns import qn

from docx.shared import Inches

from docx.enum.section import WD_ORIENT

from docx.enum.text import WD_ALIGN_PARAGRAPH

filename = filedialog.askopenfilename()

start_time = datetime.now()

df = pd.read_excel(filename,sheet_name=0)

row_number = df.shape[0]

document = Document()

information_list = ['船名航次','启运港','目的港','许可证名称','长','宽','高','毛重','实车','备注','二级库位编号','目的国家']

Template = ["VESSEL NAME &VOY. No. (船名&航次):",

"POL (装货港):",

"DESTINATION(目的港): ",

"CARGO BRAND:SAIC MOTOR ",

"MADE IN CHINA",

"CARGO DESCRIPTION: ",

"MEASUREMENTS (货物尺寸L x B x H) mm: ",

"WEIGHT(货物重量) KG: ",

"VIN NO: ",

"B/L NO: ",

"SALE REGION(销售区域):"

]

#获取年-日-月时间戳,作为word文件名

def time_now():

now = datetime.now()

now_time = now.strftime('%Y-%m-%d''-%H时%M分%S秒')

return(now_time)

#处理空值单元格

def if_none(text):

if str(text) == "nan":

return("")

else:

return(str(text))

#处理,将小数转化为整数

def get_int(x):

if x == "":

t = ""

else:

t = int(float(x))

return(t)

#根据行数需求,输出已确定的列的某一行的值

def get_row(r,c):

column_name = information_list[c-1]

information = df.loc[r-1,column_name]

text = if_none(information)

return(text)

#判断vin码是否有缺失

def vin_exist(r):

t = True

if get_row(r,9) == "":

t = False

return(t)

#输出某一行的所需所有数据

def get_car(r):

vessel_name = "\n"+Template[0]+get_row(r,1)+"\n"

pol = Template[1]+get_row(r,2)+"\n"

destination = Template[2]+get_row(r,3)+"—"+get_row(r,12)+"\n"

cargo_brand = Template[3]+"\n"

made = Template[4]+"\n"

cargo_description = Template[5]+get_row(r,4)+"\n"

measurements = Template[6]+if_none(get_int(get_row(r,5)))+"×"+if_none(get_int(get_row(r,6)))+"×"+if_none(get_int(get_row(r,7)))+"\n"

weight = Template[7] + if_none(get_int(get_row(r, 8)))+"\n"

vin_no =Template[8] + if_none(get_row(r, 9)) + "\n"

bl_sale = Template[9] + " "*18 + Template[10]+"\n"

if get_row(r, 10) == "":

bl_no = " "*25

else:bl_no = if_none(get_row(r, 10))

sale_region = " "*8+get_row(r, 11)

form = vessel_name + pol + destination + cargo_brand + made \

+ cargo_description + measurements+weight+vin_no+bl_sale+bl_no+sale_region

return (form)

#根据vin码生成一维码

def get_BC_make(r):

content = get_row(r, 9)

code128_image = code128.image(content)

img = Image.new('RGB', (code128_image.width, code128_image.height + 45), color='white')

img.paste(code128_image, (0, 4))

draw = ImageDraw.Draw(img)

text = content

font = ImageFont.truetype('times.ttf', size=80)

font.bold = True

spacing = 10

x = 66

for a in text:

warnings.filterwarnings("ignore", category=DeprecationWarning)

draw.text((x, code128_image.height+6), a, font=font, fill='black')

char_width,_ = draw.textsize(a, font=font)

x += char_width + int(spacing)

return(img)

#获取第一个二维码的数据

def get_QR_1(r):

content = get_row(r, 9) + "#" + get_row(r, 1) \

+ "#" + get_row(r, 12) + "#" + get_row(r, 2) + "#" + get_row(r, 3)

return(content)

#获取第二个二维码的数据

def get_QR_2(r):

content = get_QR_1(r)+"#"+get_row(r, 10) +"#"+get_row(r, 11)

return(content)

#设置word页面为横版

def Flip_docx(section):

section.page_width = Inches(8.27)

section.page_height = Inches(11.69)

section.top_margin = Inches(1.3)

section.bottom_margin = Inches(0.2)

section.left_margin = Inches(1.5)

section.right_margin = Inches(1)

new_width, new_height = section.page_height, section.page_width

section.orientation = WD_ORIENT.LANDSCAPE

section.page_width = new_width

section.page_height = new_height

#生成对应二维码

def QRcode_make(r,n):

if n == 1:

content = get_QR_1(r)

else:

content = get_QR_2(r)

# 创建 QRCode 对象

qr = QRCode(version=2, error_correction=constants.ERROR_CORRECT_L, box_size=10, border=1)

# 添加数据

qr.add_data(content)

# 生成 QR 码

qr.make(fit=True)

# 创建图像

img = qr.make_image(fill_color="black", back_color="white")

# 将图像转换为 RGBA 模式

img = img.convert("RGBA")

return(img)

# 转化并按嵌入式写入图像

def image_img(img,image,w,h):

if w ==0:

w = img.width

if h == 0:

h = img.height

buffer = BytesIO()

img.save(buffer, format="png")

image.add_picture(buffer, width=Inches(w), height=Inches(h))

image.alignment = WD_ALIGN_PARAGRAPH

# 将信息添加到XML元素中,并将其设置为中文编码

def write_text(r,p):

text = get_car(r)

run = p.add_run('')

run.text = text

element = run._element

rPr = element.get_or_add_rPr()

rFonts = rPr.get_or_add_rFonts()

rFonts.set(qn('w:eastAsia'), 'Arial')

#核心函数,将一维码,二维码,文本以合适格式写入word

def word_write(r):

doc = document

section = document.sections[r-1]

if r == 1:

Flip_docx(section)

p = doc.add_paragraph()

image_1 = p.add_run()

img_1 = get_BC_make(r)

img_n = Image.new('RGB', (300, 300), (255, 255, 255))

img_2 = QRcode_make(r,1)

image_img(img_1, image_1, 5, 1.6)

image_img(img_n, image_1, 1.5, 1.6)

image_img(img_2, image_1, 1.6, 1.6)

# 创建XML元素,并设置字体和字号

p.style.font.name = 'times'

p.style.font.size = Inches(0.25)

p.style.font.bold = False

write_text(r,p)

img_3 = QRcode_make(r,2)

buffer = BytesIO()

img_3.save(buffer, format="png")

#调用大佬的包,以浮动式写入图片

add_float_picture(p, buffer, width=Inches(1.6), pos_x=Inches(8.02), pos_y=Inches(5.6))

if r < row_number:

doc.add_section()

else:

doc.save(f"{time_now()}-shipping_info.docx")

#主函数

def run():

exe_time = 0

vin_error = ""

for r in range(1,row_number+1):

print(r)

if not vin_exist(r):

vin_error += "表格第" + str(r) + "行数据,缺失vin码"+"\n"

word_write(r)

print("生成成功")

exe_time = r

ending_time = datetime.now()

time = ending_time - start_time

print(f'运行时间为:{time}')

msgbox("已经成功生成了" + str(exe_time) + "张唛头")

if vin_error == "":

pass

else:

msgbox(vin_error)

if __name__ == '__main__':

run()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值