统信平台python给PDF添加页码(爆快新办法!)

'''

1、全新安装统信1070,并更新系统

2、进入开发者模式

3、改变python默认版本为3.7.3

在终端输入:

rm -f /usr/bin/python

ln /usr/bin/python3.7 /usr/bin/python

python -V

4、安装pip

在终端输入:

apt-get update

apt-get install wget

wget https://bootstrap.pypa.io/get-pip.py

python get-pip.py

pip -V

5、安装idle

在终端输入:sudo apt install idle

6、安装PyPDF2和reportlab两个Python库

在终端输入:pip install PyPDF2 reportlab

7、安装PyMuPDF

在终端输入:pip install PyMuPDF==1.19.0

*python3.7对应PyMuPDF==1.19.0

8、代码打包

pyinstaller --onefile --noconsole 给PDF添加页码fitz(界面版带进度).py

'''

from PyPDF2 import PdfWriter, PdfReader

import fitz # 导入Fitz库

import datetime

import time

import os

import tkinter as tk #导入tkinter模块

from tkinter import filedialog #导入filedialog库

from tkinter import messagebox

root = tk.Tk()

root.withdraw()

input_pdf_path =
filedialog.askopenfilename(title='给PDF文件添加“1/N”页码', filetypes=[('PDF格式', '*.pdf')])

filename = os.path.splitext(input_pdf_path)[0]

if input_pdf_path:

print("选择的文件为:", input_pdf_path)

else:

print("用户取消了选择文件")

time1 = time.time()

now = datetime.datetime.now()

def update_text(text_box, message):

text_box.delete("1.0", tk.END) # 清空文本框内容

text_box.tag_config("center", justify="center")# 创建一个名为"center"的tag,设置样式为居中对齐

text_box.insert(tk.END, message, "center") # 插入新的信息

def add_page_numbers(input_pdf, output_pdf):

pdf_document = fitz.open(input_pdf)# 打开输入PDF文件

total_pages = len(pdf_document)# 获取总页数

root = tk.Tk()

screen_width = root.winfo_screenwidth()

screen_height = root.winfo_screenheight()

# 计算窗口位置

text_box_width = 100

text_box_height = 30

x = (screen_width // 2) - (text_box_width // 2)

y = (screen_height // 2) - (text_box_height // 2)

# 设置窗口位置

root.geometry(f'{text_box_width}x{text_box_height}+{x}+{y}')

root.title("添加页码.....")

root.overrideredirect(True)#去除标题栏

text_box = tk.Text(root, width=text_box_width, height=text_box_height, wrap=tk.WORD)

text_box.pack()


for page_num in range(total_pages):

page = pdf_document[page_num]


rotation = page.rotation # 获取页面旋转角度

width, height = page.mediabox.width, page.mediabox.height# 获取页面的宽度和高度

if rotation == 180:

pgx = width / 2 - 9

pgy = 30

elif rotation == 90:

pgx = width - 30

pgy = height / 2 - 9

elif rotation == 270:

pgx = 30

pgy = height / 2 - 9

else:

pgx = width / 2 - 9

pgy = height - 30

page_number_text = f"{page_num + 1} / {total_pages}"# 设置页码文本

text_rect = fitz.Rect(pgx, pgy, 100, 100)# 设置页码位置

page.insert_text(text_rect.tl, page_number_text, fontsize=25, color = (1, 0, 0), rotate=rotation)# 设置页码字体

update_text(text_box, page_number_text) # 更新文本框内容

root.update() # 更新GUI以显示最新的文本框内容

root.destroy()# 关闭文本框

# 保存到输出PDF文件

pdf_document.save(output_pdf)

pdf_document.close()

print("页码添加成功!")

time2 = time.time()

print('总共耗时:%s 秒.' %(time2 - time1))

print("保存的文件为:",output_pdf_path)

root = tk.Tk()

root.withdraw()

messagebox.showinfo("页码添加成功!", '总共耗时:%s 秒.' %(time2 - time1))

# 使用示例

output_pdf_path = f"{filename}_{now.strftime('%Y%m%d%H%M%S')}_加页码.pdf"

add_page_numbers(input_pdf_path, output_pdf_path)