I have a main folder with some.xlsx, .ipynb, .jpeg and some subfolders in it. Now I want to convert all my.xlsx files in my main folder to PDFs.
It is a routine work that I have to do everyday, I would appreciate if you teach me how to do it in python.
*all the files have some data in the first sheet of the workbook
Thank you
Is there anything you have already tried?
I suggest testing out pywin32.
python3 -m pip install pywin32
import win32com.client
from pywintypes import com_error
# Path to original excel file
WB_PATH = r'~/path/to/file.xlsx'
# PDF path when saving
PATH_TO_PDF = r'~/path/to/file.pdf'
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = False
try:
print('Start conversion to PDF')
# Open
wb = excel.Workbooks.Open(WB_PATH)
# Specify the sheet you want to save by index. 1 is the first (leftmost) sheet.
ws_index_list = [1,2,3,4,5,6,7,8,9,10,11,12]
wb.WorkSheets(ws_index_list).Select()
# Save
wb.ActiveSheet.ExportAsFixedFormat(0, PATH_TO_PDF)
except com_error as e:
print('failed.')
else:
print('Succeeded.')
finally:
wb.Close()
excel.Quit()
try like this:
saveFormat = self.SaveFormat
workbook = self.Workbook(self.dataDir + "Book1.xls")
#Save the document in PDF format
workbook.save(self.dataDir + "OutBook1.pdf", saveFormat.PDF)
\# Print message
print "\n Excel to PDF conversion performed successfully."
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.