I’m currently trying to create PDF from Excel file in Python, with the hour and date of creation of the PDF on the name’s file. But I’m stuck with the relative path:
output_file = str(path)+"\Perception-demandeur.pdf"
How could I add a date object in my relative path? Just below part of my code.
path = Path(__file__).parent input_file = str(path)+"\preparation-pdf-perception.xlsx" # give your file name with valid path persopdf = now.strftime("%d/%m/%Y-%H:%M:%S") output_file = str(path)+"\Perception-demandeur.pdf" #output_file = f"/10DBA/Perception-demandeur-{persopdf}.pdf" # give valid output file name and path app = client.DispatchEx("Excel.Application") app.Interactive = False app.Visible = False Workbook = app.Workbooks.Open(input_file) try: Workbook.ActiveSheet.ExportAsFixedFormat(0, output_file) except Exception as e: print("Failed to convert in PDF format.Please confirm environment meets all the requirements and try again") print(str(e))
Advertisement
Answer
Based on your comment I’ll try to make an answer:
You have the following as the formating: now.strftime("%d/%m/%Y-%H:%M:%S")
This would, as of right now, produce the string “14/01/2022-10:50:46”.
But windows does not allow slashes nor colons in file names. So you would have to find another date format. Maybe something like now.strftime("%d%m%Y %H-%M-%S")
, which will get you “14012022 10-50-46”.
Or, to make it more sortable: now.strftime("%Y%m%d %H-%M-%S")
for “20220114 10-50-46”