Created
August 14, 2023 20:30
-
-
Save Anas-jaf/934bc4e803bc733a5d72543f90f5d7eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import win32com.client as win32 | |
import xlwings as xw | |
def excelActiveSheet_to_pdf(excel_file_path, pdf_file_name, num_pages_to_export): | |
app = win32.DispatchEx("Excel.Application") | |
app.Interactive = False | |
app.Visible = False | |
workbook = app.Workbooks.Open(excel_file_path) | |
workbook.ActiveSheet.ExportAsFixedFormat(0 , pdf_file_name) | |
workbook.Close() | |
app.Quit() | |
if __name__ == "__main__": | |
num_pages_to_export = 2 | |
current_directory = os.getcwd() | |
for excel_file_name in os.listdir(current_directory): | |
if excel_file_name.endswith(".xlsx"): | |
excel_file_path = current_directory+ "/" + excel_file_name | |
pdf_file_name = os.path.splitext(excel_file_name)[0] + ".pdf" | |
pdf_file_path = current_directory + '/' + pdf_file_name | |
excelActiveSheet_to_pdf(excel_file_path, pdf_file_path, num_pages_to_export) | |
print(f"Converted {excel_file_name} to {pdf_file_name}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment