1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import win32com.client,os def excel_to_pdf(excel_path, pdf_path): excel = win32com.client.Dispatch("Excel.Application") excel.Visible = False wb = excel.Workbooks.Open(excel_path) wb.ExportAsFixedFormat( Type=0, Filename=pdf_path, Quality=win32com.client.constants.xlQualityStandard, IncludeDocProperties=True, IgnorePrintAreas=False, OpenAfterPublish=False ) wb.Close(SaveChanges=False) excel.Quit()
def excel_file(): path = os.getcwd() files = os.listdir(path) if not os.path.exists("pdf"): os.mkdir("pdf") excelfiles = [f for f in files if not f.startswith(("~$")) and f.endswith((".xlsx"))] for file in excelfiles: excel_path = os.path.join(path,file) pdf_name = os.path.splitext(file)[0] pdf_path = path + "\\" + "pdf" + "\\" + pdf_name + ".pdf" excel_to_pdf(excel_path, pdf_path) print(excel_path+"格式转换完成") excel_file()
|