from win32com import client
def to_pdf(filename,folder): df=pd.read_excel(target_path,sheet_name='总目录') Rukudan = sorted(list(set(list(df['编号'])))) filelist=os.listdir(folder) for i in Rukudan: i=i+'.pdf' #保存过PDF的就不保存了,这点需要注意, 如果想重新保存需要先删掉对应的PDF if i not in filelist: exportfile = os.path.join(folder, i) # Open Microsoft Excel excel = client.Dispatch("Excel.Application") # Read Excel File sheets = excel.Workbooks.Open(filename) work_sheets = sheets.Worksheets[i[-10:-4]] # Convert into PDF File work_sheets.ExportAsFixedFormat(0, exportfile) print('保存 PDF 文件:', exportfile) # close Excel File sheets.Close(False)