I have some code to convert an excel file from excel to PDF. Although I know that openpyxl has methods to assign column width value, wrap text and add cell border, I am looking for a way to do it with win32com module. This is because I already have opened the Excel file with win32com and I can save execution time by not having to load the Excel file with openpyxl again.
# Import Module
from win32com import client
# Open Microsoft Excel
excel = client.gencache.EnsureDispatch('Excel.Application')
# Make excel work in the background without appearing
excel.Visible = False
# Read Excel File
wb = excel.Workbooks.Open(r'C:\Spaced out data.xlsx')
ws = wb.Worksheets('Sheet1')
# Adjust page setup to landscape
ws.PageSetup.Orientation = 1
# Set Zoom to false because you want to fit all columns to the width of 1 page.
ws.PageSetup.Zoom = False
# Allow rows to be on multiple pages
ws.PageSetup.FitToPagesTall = False
# Fit all columns to the width of 1 page.
ws.PageSetup.FitToPagesWide = 1
# Convert into PDF File
ws.ExportAsFixedFormat(0, r'C:\Spaced out data.pdf')
wb.Close(SaveChanges=False)
excel.Quit()