分享给有需要的人。
功能是预先在EXCEL里指定对应需求的数据,根据选择列来生成二维码并放置在PDF图纸的左下角。
image.png (24.14 KB, 下载次数: 0)
下载附件
01
2024-11-22 10:26 上传
image.png (120.85 KB, 下载次数: 0)
下载附件
02
2024-11-22 10:31 上传
源码如下:
[color=]import
[color=]tkinter
[color=]as
[color=]tk
[color=]from
[color=]tkinter
[color=]import
[color=]filedialog
,
[color=]messagebox
[color=]from
[color=]PIL
[color=]import
[color=]Image
,
[color=]ImageDraw
,
[color=]ImageFont
[color=]import
[color=]pandas
[color=]as
[color=]pd
[color=]import
[color=]fitz
[color=]# PyMuPDF
[color=]import
[color=]os
[color=]import
[color=]io
[color=]import
[color=]qrcode
[color=]#基本数据获取代码段
[color=]def
[color=]browse_folder
(
[color=]entry_var
):
[color=]#浏览文件夹对话框,选择后更新entry变量
[color=]folder_path
=
[color=]filedialog
.
[color=]askdirectory
()
[color=]entry_var
.set(
[color=]folder_path
)
[color=]def
[color=]browse_file
(
[color=]entry_var
,
[color=]filetypes
):
[color=]#浏览文件对话框,选择后更新entry变量,支持特定文件类型
[color=]file_path
=
[color=]filedialog
.
[color=]askopenfilename
(
[color=]filetypes
=
[color=]filetypes
)
[color=]entry_var
.set(
[color=]file_path
)
[color=]if
[color=]file_path
:
[color=]# 如果选择了文件,则尝试读取并更新列名下拉菜单
[color=]global
[color=]df
[color=]df
=
[color=]pd
.
[color=]read_excel
(
[color=]file_path
)
[color=]update_column_options
()
[color=]def
[color=]update_column_options
():
[color=]#更新列名下拉菜单的选项
[color=]global
[color=]file_name_dropdown
,
[color=]date_dropdown
,
[color=]qty_dropdown
,
[color=]order_dropdown
,
[color=]column_options
[color=]column_options
=
[color=]df
.
[color=]columns
.
[color=]tolist
()
[color=]file_name_dropdown
.
[color=]set
(
[color=]column_options
[
[color=]0
])
[color=]# 设置默认选中第一个列为文件名列
[color=]date_dropdown
.
[color=]set
(
[color=]column_options
[
[color=]0
])
[color=]# 设置默认选中第一个列为
[color=]qty_dropdown
.
[color=]set
(
[color=]column_options
[
[color=]0
])
[color=]# 设置默认选中第一个列为
[color=]order_dropdown
.
[color=]set
(
[color=]column_options
[
[color=]0
])
[color=]# 设置默认选中第一个列为
[color=]file_name_dropdown_menu
[
[color=]'menu'
].delete(
[color=]0
,
[color=]'end'
)
[color=]# 清除现有选项
[color=]date_dropdown_menu
[
[color=]'menu'
].delete(
[color=]0
,
[color=]'end'
)
[color=]# 清除现有选项
[color=]qty_dropdown_menu
[
[color=]'menu'
].delete(
[color=]0
,
[color=]'end'
)
[color=]# 清除现有选项
[color=]order_dropdown_menu
[
[color=]'menu'
].delete(
[color=]0
,
[color=]'end'
)
[color=]# 清除现有选项
[color=]for
[color=]option
[color=]in
[color=]column_options
:
[color=]file_name_dropdown_menu
[
[color=]'menu'
].add_command(
[color=]label
=
[color=]option
,
[color=]command
=
[color=]tk
.
[color=]_setit
(
[color=]file_name_dropdown
,
[color=]option
))
[color=]date_dropdown_menu
[
[color=]'menu'
].add_command(
[color=]label
=
[color=]option
,
[color=]command
=
[color=]tk
.
[color=]_setit
(
[color=]date_dropdown
,
[color=]option
))
[color=]qty_dropdown_menu
[
[color=]'menu'
].add_command(
[color=]label
=
[color=]option
,
[color=]command
=
[color=]tk
.
[color=]_setit
(
[color=]qty_dropdown
,
[color=]option
))
[color=]order_dropdown_menu
[
[color=]'menu'
].add_command(
[color=]label
=
[color=]option
,
[color=]command
=
[color=]tk
.
[color=]_setit
(
[color=]order_dropdown
,
[color=]option
))
[color=]root
.
[color=]update_idletasks
()
[color=]# 更新界面
[color=]#功能函数代码段
[color=]def
[color=]add_image_to_pdf
(
[color=]pdf_path
,
[color=]output_path
,
[color=]qr_value
,
[color=]customer_input
,
[color=]batch_input
,
[color=]date_value
,
[color=]qty_value
):
[color=]# 打开PDF文件
[color=]doc
=
[color=]fitz
.
[color=]open
(
[color=]pdf_path
)
[color=]page
=
[color=]doc
.
[color=]load_page
(
[color=]0
)
[color=]page_rect
=
[color=]page
.
[color=]rect
[color=]width
=
[color=]page_rect
.width
[color=]height
=
[color=]page_rect
.height
[color=]rotation
=
[color=]page
.
[color=]rotation
[color=]# 合并文本
[color=]text
= [
[color=]f
[color=]"客户:
[color=]{
[color=]customer_input
[color=]}
[color=]"
,
[color=]f
[color=]"交期:
[color=]{
[color=]date_value
[color=]}
[color=]"
,
[color=]f
[color=]"批次:
[color=]{
[color=]batch_input
[color=]}
[color=]"
,
[color=]f
[color=]"数量:
[color=]{
[color=]qty_value
[color=]}
[color=] 件"
]
[color=]# 创建并保存图片
[color=]image_size
=
[color=]200
[color=]line_width
=
[color=]1
[color=]font_size
=
[color=]20
[color=]font_path
=
[color=]"C:/Windows/Fonts/msyhbd.ttc"
[color=]img
=
[color=]Image
.
[color=]new
(
[color=]"RGBA"
, (
[color=]image_size
,
[color=]image_size
), (
[color=]255
,
[color=]255
,
[color=]255
,
[color=]0
))
[color=]draw
=
[color=]ImageDraw
.
[color=]Draw
(
[color=]img
)
[color=]for
[color=]i
[color=]in
[color=]range
(
[color=]line_width
):
[color=]draw
.
[color=]rectangle
([(
[color=]i
,
[color=]i
), (
[color=]image_size
-
[color=]i
-
[color=]1
,
[color=]image_size
-
[color=]i
-
[color=]1
)],
[color=]outline
=(
[color=]0
,
[color=]0
,
[color=]0
))
[color=]font
=
[color=]ImageFont
.
[color=]truetype
(
[color=]font_path
,
[color=]font_size
)
[color=]text_height
=
[color=]font
.
[color=]getbbox
(
[color=]'A'
)[
[color=]3
]
[color=]row_spacing
= (
[color=]image_size
-
[color=]2
*
[color=]line_width
) /
[color=]5
[color=]for
[color=]idx
,
[color=]line
[color=]in
[color=]enumerate
(
[color=]text
):
[color=]text_position
= (
[color=]line_width
+
[color=]10
,
[color=]line_width
+
[color=]row_spacing
*(
[color=]idx
+
[color=]1
) -
[color=]text_height
/
[color=]2
)
[color=]draw
.
[color=]text
(
[color=]text_position
,
[color=]line
,
[color=]fill
=(
[color=]0
,
[color=]0
,
[color=]0
),
[color=]font
=
[color=]font
)
[color=]# 计算图片最大可缩放尺寸,保持原始宽高比
[color=]scaled_img_width
=
[color=]int
(
[color=]page_rect
.width *
[color=]0.14
)
[color=]scaled_img_height
=
[color=]int
(
[color=]page_rect
.height *
[color=]0.14
)
[color=]scaled_img_size
=
[color=]min
(
[color=]scaled_img_width
,
[color=]scaled_img_height
)
[color=]# 重新调整图片尺寸
[color=]img_resized
=
[color=]img
.
[color=]resize
((
[color=]scaled_img_size
,
[color=]scaled_img_size
),
[color=]resample
=
[color=]Image
.LANCZOS)
[color=]# 将PIL图像转换为BytesIO对象,以便插入PDF
[color=]img_byte_arr
=
[color=]io
.
[color=]BytesIO
()
[color=]img_resized
.
[color=]save
(
[color=]img_byte_arr
,
[color=]format
=
[color=]'PNG'
)
[color=]img_byte_arr
=
[color=]img_byte_arr
.
[color=]getvalue
()
[color=]# 生成二维码
[color=]qr
=
[color=]qrcode
.QRCode(
[color=]version
=
[color=]1
,
[color=]error_correction
=
[color=]qrcode
.constants.ERROR_CORRECT_L,
[color=]box_size
=
[color=]10
,
[color=]border
=
[color=]0
,
)
[color=]qr
.add_data(
[color=]qr_value
)
[color=]qr
.make(
[color=]fit
=
[color=]True
)
[color=]qr_img
=
[color=]qr
.make_image(
[color=]fill_color
=
[color=]"black"
,
[color=]back_color
=
[color=]"transparent"
)
[color=]# 计算二维码尺寸为页面大小的20%
[color=]#qr_size = qr.modules_count * qr.box_size
[color=]qr_width_percent
=
[color=]int
(
[color=]page_rect
.width *
[color=]0.12
)
[color=]qr_height_percent
=
[color=]int
(
[color=]page_rect
.height *
[color=]0.12
)
[color=]scaled_qr_size
=
[color=]min
(
[color=]qr_width_percent
,
[color=]qr_height_percent
)
[color=]qr_img
=
[color=]qr_img
.resize((
[color=]scaled_qr_size
,
[color=]scaled_qr_size
),
[color=]resample
=
[color=]Image
.LANCZOS)
[color=]# 将二维码图片转换为BytesIO对象
[color=]qr_img_byte_arr
=
[color=]io
.
[color=]BytesIO
()
[color=]qr_img
.save(
[color=]qr_img_byte_arr
,
[color=]format
=
[color=]'PNG'
)
[color=]qr_img_byte_arr
=
[color=]qr_img_byte_arr
.
[color=]getvalue
()
[color=]# 根据页面方向调整二维码位置
[color=]if
[color=]width
>
[color=]height
:
[color=]# 页面横向
[color=]if
[color=]rotation
==
[color=]0
[color=]or
[color=]rotation
==
[color=]180
:
[color=]#水印位置
[color=]rect
=
[color=]fitz
.
[color=]Rect
(
[color=]width
-
[color=]scaled_img_size
,
[color=]0
,
[color=]width
,
[color=]scaled_img_size
)
[color=]# 右上角ok
[color=]page
.insert_image(
[color=]rect
,
[color=]stream
=
[color=]img_byte_arr
,
[color=]overlay
=
[color=]True
,
[color=]keep_proportion
=
[color=]True
,
[color=]rotate
=
[color=]0
)
[color=]#二维码位置
[color=]img_rect
=
[color=]fitz
.
[color=]Rect
(
[color=]0
,
[color=]height
-
[color=]scaled_qr_size
,
[color=]scaled_qr_size
,
[color=]height
)
[color=]# 左下角ok
[color=]page
.insert_image(
[color=]img_rect
,
[color=]stream
=
[color=]qr_img_byte_arr
,
[color=]overlay
=
[color=]True
,
[color=]keep_proportion
=
[color=]True
,
[color=]rotate
=
[color=]0
)
[color=]elif
[color=]rotation
==
[color=]90
[color=]or
[color=]rotation
==
[color=]270
:
[color=]#水印位置
[color=]rect
=
[color=]fitz
.
[color=]Rect
(
[color=]height
-
[color=]scaled_img_size
,
[color=]0
,
[color=]height
,
[color=]scaled_img_size
)
[color=]# 右上角ok
[color=]page
.insert_image(
[color=]rect
,
[color=]stream
=
[color=]img_byte_arr
,
[color=]overlay
=
[color=]True
,
[color=]keep_proportion
=
[color=]True
,
[color=]rotate
=
[color=]0
)
[color=]#二维码位置
[color=]img_rect
=
[color=]fitz
.
[color=]Rect
(
[color=]0
,
[color=]0
,
[color=]scaled_qr_size
,
[color=]scaled_qr_size
)
[color=]# 左上角ok
[color=]page
.insert_image(
[color=]img_rect
,
[color=]stream
=
[color=]qr_img_byte_arr
,
[color=]overlay
=
[color=]True
,
[color=]keep_proportion
=
[color=]True
,
[color=]rotate
=
[color=]0
)
[color=]if
[color=]width