鱼C论坛

 找回密码
 立即注册
查看: 1305|回复: 9

[已解决]PDF转换问题?合成后尺寸变了

[复制链接]
发表于 2024-6-11 14:48:34 | 显示全部楼层 |阅读模式
15鱼币
本帖最后由 任申猴 于 2024-6-11 14:51 编辑

源文件:竖向的在浏览器打开100%状态看着是正常的;
1.png

由于需要把它转为图片在转成PDF(避免抠图)
  1. import os
  2. import fitz
  3. from PIL import Image
  4. import shutil
  5. import re
  6. import win32com.client as win32

  7. def excel_to_pdf():
  8.     """当前黑白版转PDF"""
  9.     current_directory = os.getcwd()
  10.     xls_files = [f for f in os.listdir(current_directory) if f.endswith('.xls')]
  11.     if not xls_files:
  12.         print('当前目录下没有 .xls 文件')
  13.         return

  14.     xls_file_names = [os.path.splitext(f)[0] for f in xls_files]
  15.     xls_path = os.path.join(current_directory, xls_files[0])
  16.     pdf_out_path = current_directory

  17.     if not os.path.exists(pdf_out_path):
  18.         os.makedirs(pdf_out_path)

  19.     excel = win32.gencache.EnsureDispatch('Excel.Application')
  20.     excel.Visible = False
  21.     workbook = excel.Workbooks.Open(xls_path)
  22.     pdf_path = os.path.join(pdf_out_path, f"{xls_file_names[0]}_报告.pdf")
  23.     workbook.ExportAsFixedFormat(0, pdf_path)
  24.     workbook.Close(False)
  25.     excel.Application.Quit()

  26.     result = re.search(r'\d+', pdf_path)
  27.     if result:
  28.         number_only = result.group()
  29.     pdf_name = f'LTJC{number_only}_报告.pdf'
  30.     rightmost_index = len(pdf_name) - 4
  31.     folder_name = pdf_name[:rightmost_index]

  32.     if not os.path.exists(folder_name):
  33.         os.makedirs(folder_name)
  34.         print(f"文件夹 '{folder_name}' 已成功创建。")
  35.     else:
  36.         print(f"文件夹 '{folder_name}' 已经存在。")

  37.     doc = fitz.open(pdf_name)

  38.    
  39.     dpi = 300  
  40.     zoom = dpi / 72  # PDF 默认是 72 DPI
  41.     mat = fitz.Matrix(zoom, zoom)

  42.     for page_num in range(len(doc)):
  43.         page = doc[page_num]
  44.         pix = page.get_pixmap(matrix=mat)
  45.         output = os.path.join(folder_name, f"page_{page_num}.png")
  46.         pix.save(output)

  47.     doc.close()

  48.     if os.path.exists(pdf_name):
  49.         os.remove(pdf_name)
  50.         print(f'文件 {pdf_name} 已成功删除')
  51.     else:
  52.         print(f'文件 {pdf_name} 不存在')

  53.     image_folder = folder_name
  54.     image_files = [f for f in os.listdir(image_folder) if f.endswith('.png')]
  55.     image_files.sort(key=lambda x: int(x.split('_')[1].split('.')[0]))

  56.     images = []
  57.     for image_file in image_files:
  58.         img_path = os.path.join(image_folder, image_file)
  59.         img = Image.open(img_path)
  60.         if img.mode == 'RGBA':
  61.             img = img.convert('RGB')
  62.         print(dpi)
  63.         a4_width, a4_height = (2480, 3508)
  64.         # if dpi == 600:
  65.         #     a4_width, a4_height = (4960, 7016)
  66.         # elif dpi == 1200:
  67.         #     a4_width, a4_height = (9920, 14032)

  68.         img = img.resize((a4_width, a4_height), Image.Resampling.LANCZOS)
  69.         jpg_path = os.path.join(image_folder, f"page_{image_file.split('_')[1].split('.')[0]}.png")
  70.         img.save(jpg_path, format='JPEG', quality=85)

  71.         images.append(Image.open(jpg_path))

  72.     pdf_path = pdf_name[:15] + '_报告.pdf'
  73.     images[0].save(pdf_path, save_all=True, append_images=images[1:])

  74.     folder_path = folder_name
  75.     if os.path.exists(folder_path):
  76.         shutil.rmtree(folder_path)
  77.         print(f'文件夹 {folder_path} 已成功删除')
  78.     else:
  79.         print(f'文件夹 {folder_path} 不存在')

  80.     print(number_only)

  81.     return pdf_path, xls_file_names
  82. excel_to_pdf()
复制代码



转换出来了 ,图片变大了 ,在浏览器打开100%状态下,大了几倍,但我设置的是300dpi  ,请问大佬这是为什么?

2.png


最佳答案
2024-6-11 14:48:35
  1. images[0].save(output_pdf_path, save_all=True, append_images=images[1:],resolution=300,quality=100,optims=True)
复制代码

解决了,需要把这几个参数指定了

最佳答案

查看完整内容

解决了,需要把这几个参数指定了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-6-11 14:48:35 | 显示全部楼层    本楼为最佳答案   
  1. images[0].save(output_pdf_path, save_all=True, append_images=images[1:],resolution=300,quality=100,optims=True)
复制代码

解决了,需要把这几个参数指定了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-6-11 18:32:50 | 显示全部楼层
不二如是 发表于 2024-6-11 15:46
貌似,dpi没有生效,稍等我来测试下代码

咋看不了回复??
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-6-12 09:08:04 | 显示全部楼层

还是白版,AI生成的那个能看见,啥情况
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2024-6-12 09:52:26 | 显示全部楼层
Twilight6 发表于 2024-6-12 09:33
是不是因为你 resize 重新设置了尺寸了?

不是,注释了还是,变大了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-15 03:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表