问题遇到的现象和发生背景
想把1张A3的试卷拆分成两张A4纸
问题相关代码,请勿粘贴截图
import PyPDF2
input_file_path = 'C:\Users\longj\Desktop\A3\a.pdf'
input_file = PyPDF2.PdfFileReader(open(input_file_path, 'rb')) # 导入源文件
A3总页数 = input_file.getNumPages() # 取得页数
print(A3总页数)
#取得第一页
i = 0
for A3页数 in range(A3总页数):
print('这是A3页第{}页'.format(A3页数))
for A4页数 in range(2):#一张A3可分为2页A4
print('这是A4第{}页'.format(A4页数))
output_file_path = 'C:\Users\longj\Desktop\A3\{}{}.pdf'.format(A3页数,A4页数)
print('I的值是:{}'.format(i))
截图 = input_file.getPage(i)
截图.mediaBox.lowerLeft = (0, 0) # 坐标系的原点在左下角
截图.mediaBox.lowerRight = (600, 0)
截图.mediaBox.upperLeft = (0, 830)
截图.mediaBox.upperRight = (600, 830)
页面 = PyPDF2.PdfFileWriter() # 输出PDF
页面.addPage(截图) # 这个添加功能,一页只可能一个,不能复用,否则会产生多个重复文件
页面.write(open(output_file_path, 'ab'))
截图.mediaBox.lowerLeft = (600,0) #坐标系的原点在左下角
截图.mediaBox.lowerRight = (1200,0)
截图.mediaBox.upperLeft = (600,1000)
截图.mediaBox.upperRight = (1200,1000)
页面1 = PyPDF2.PdfFileWriter() # 输出PDF
页面1.addPage(截图) # 这个输出,一页只可能一个,不能复用,否则会产生多个重复文件
页面1.write(open(output_file_path, 'ab'))
i = i + 1
我想要达到的结果
导入A3试卷后,自己按总页数分拆,每张A3纸拆分成2张A4纸。生成的PDF文件(A4)按顺序命名,最好能把加生成的A4文件合部合并。