#python3.12
#边距占比marginp
marginp=0.00
# 使用示例
input=r"I:\2025-l2-topics-combined.pdf"
input=input.replace("\\","/")
output_a3="I:/GOOD.pdf"
import pymupdf
import fitz # 安装:pip install pymupdf
def convert_a4_to_a3(input_path, output_path):
# 打开原始PDF
src_doc = fitz.open(input_path)
# 创建新PDF文档
dst_doc = fitz.open()
for page in src_doc:
# 定义横版A3页面尺寸(宽1190.4,高841.68)
a3_width = 1190.4
a3_height = 841.68
new_page = dst_doc.new_page(width=a3_width, height=a3_height)
# 计算原A4内容在A3左半边的位置(保留边距5%)
margin = a3_width * marginp # 左右各留5%边距
content_width = (a3_width / 2) - 2 * margin
content_height = a3_height - 2 * margin
# 定义内容显示区域(左半部分居中)
target_rect = fitz.Rect(
margin,
margin,
(a3_width / 2) - margin,
a3_height - margin
)
# 将原页面内容插入到新位置(自动缩放适应目标区域)
new_page.show_pdf_page(
target_rect, # 目标区域
src_doc, # 源文档
page.number, # 源页码
keep_proportion=True # 保持宽高比
)
# 保存处理后的PDF
dst_doc.save(output_path)
print(f"处理完成,保存至:{output_path}")
convert_a4_to_a3(input, output_a3)