0

I had to generate thousands of pdf from single excel file after some simple manipulations in the excel sheet, hence I wrote an automation code in python using win32 API.

But when I use the below code, the image present in excel is getting blurry in the pdf.

from openpyxl import  load_workbook
from win32com import client
import win32api
import pathlib
wb = load_workbook("xyz.xlsx") ###Excel file name
sheet1 = wb["sheet1"] ###This is the sheet which you want to print
sheet2 = wb["sheet2"] ###This is the sheet which contains the number of pdf to be generated
sheet3 = wb["Sheet3"] ### This is the sheet in which you have to make changes 
for i in range(2,150): 
    val=sheet2.cell(row=i,column=1).value  
    sheet3.cell(row=3,column=2).value = val 
    wb.save("xyz.xlsx") 
    filename = str(val)+".pdf"
    excel_file = "xyz.xlsx" 
    pdf_file = filename
    excel_path = str(pathlib.Path.cwd() / excel_file)
    pdf_path = str(pathlib.Path.cwd() / pdf_file)
    excel = client.DispatchEx("Excel.Application")
    excel.Visible = 0
    wbs = excel.Workbooks.Open(excel_path)
    ws =wbs.Worksheets['sheet1'] 
    try:
        ws.SaveAs(pdf_path, FileFormat=57)
    except Exception as e:
        print("Failed to convert")
        print(str(e))
    finally:
        wbs.Close(SaveChanges=False)        
        excel.Quit()

This code is perfectly fine when it comes to functionality, but the image in excel sheet is getting blurry in pdf. Whereas, when I save the excel sheet to pdf manually, the image quality remains same as in excel.

Can someone help me, how I can keep the image resolution same as in excel using the above automation.

  • Hmm. Have you tried using `ExportAsFixedFormat`, demonstrated for example [here](https://stackoverflow.com/questions/39442476/win32com-saving-exporting-multiple-sheets-as-pdf). – BigBen Apr 02 '21 at 18:19
  • @BigBen I tried using it, the quality of image increased a bit but not to the best extent. – NIKHIL NAIR Apr 21 '21 at 11:02

0 Answers0