本帖最后由 任申猴 于 2024-6-11 14:51 编辑
源文件:竖向的在浏览器打开100%状态看着是正常的;
由于需要把它转为图片在转成PDF(避免抠图)import os
import fitz
from PIL import Image
import shutil
import re
import win32com.client as win32
def excel_to_pdf():
"""当前黑白版转PDF"""
current_directory = os.getcwd()
xls_files = [f for f in os.listdir(current_directory) if f.endswith('.xls')]
if not xls_files:
print('当前目录下没有 .xls 文件')
return
xls_file_names = [os.path.splitext(f)[0] for f in xls_files]
xls_path = os.path.join(current_directory, xls_files[0])
pdf_out_path = current_directory
if not os.path.exists(pdf_out_path):
os.makedirs(pdf_out_path)
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = False
workbook = excel.Workbooks.Open(xls_path)
pdf_path = os.path.join(pdf_out_path, f"{xls_file_names[0]}_报告.pdf")
workbook.ExportAsFixedFormat(0, pdf_path)
workbook.Close(False)
excel.Application.Quit()
result = re.search(r'\d+', pdf_path)
if result:
number_only = result.group()
pdf_name = f'LTJC{number_only}_报告.pdf'
rightmost_index = len(pdf_name) - 4
folder_name = pdf_name[:rightmost_index]
if not os.path.exists(folder_name):
os.makedirs(folder_name)
print(f"文件夹 '{folder_name}' 已成功创建。")
else:
print(f"文件夹 '{folder_name}' 已经存在。")
doc = fitz.open(pdf_name)
dpi = 300
zoom = dpi / 72 # PDF 默认是 72 DPI
mat = fitz.Matrix(zoom, zoom)
for page_num in range(len(doc)):
page = doc[page_num]
pix = page.get_pixmap(matrix=mat)
output = os.path.join(folder_name, f"page_{page_num}.png")
pix.save(output)
doc.close()
if os.path.exists(pdf_name):
os.remove(pdf_name)
print(f'文件 {pdf_name} 已成功删除')
else:
print(f'文件 {pdf_name} 不存在')
image_folder = folder_name
image_files = [f for f in os.listdir(image_folder) if f.endswith('.png')]
image_files.sort(key=lambda x: int(x.split('_')[1].split('.')[0]))
images = []
for image_file in image_files:
img_path = os.path.join(image_folder, image_file)
img = Image.open(img_path)
if img.mode == 'RGBA':
img = img.convert('RGB')
print(dpi)
a4_width, a4_height = (2480, 3508)
# if dpi == 600:
# a4_width, a4_height = (4960, 7016)
# elif dpi == 1200:
# a4_width, a4_height = (9920, 14032)
img = img.resize((a4_width, a4_height), Image.Resampling.LANCZOS)
jpg_path = os.path.join(image_folder, f"page_{image_file.split('_')[1].split('.')[0]}.png")
img.save(jpg_path, format='JPEG', quality=85)
images.append(Image.open(jpg_path))
pdf_path = pdf_name[:15] + '_报告.pdf'
images[0].save(pdf_path, save_all=True, append_images=images[1:])
folder_path = folder_name
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
print(f'文件夹 {folder_path} 已成功删除')
else:
print(f'文件夹 {folder_path} 不存在')
print(number_only)
return pdf_path, xls_file_names
excel_to_pdf()
转换出来了 ,图片变大了 ,在浏览器打开100%状态下,大了几倍,但我设置的是300dpi ,请问大佬这是为什么?
images[0].save(output_pdf_path, save_all=True, append_images=images[1:],resolution=300,quality=100,optims=True)
解决了,需要把这几个参数指定了
|