I have a folder of xlsx files that I want to edit (add columns...) and save with a new name. This code here works but saves in a new directory with same name. How can I modify the second last line of the code to give it an extension to the original name (i.e. originalname_addtext.xlsx)
from pathlib import Path
from openpyxl import load_workbook
cwd = Path(os.getcwd())
source = cwd / Path('/Users/lidia/Desktop/xlsx/')
dest = cwd / Path('/Users/lidia/Desktop/output/')
for filename in source.glob('*.xlsx'):
.....
wb.save(dest / filename.name)
wb.close()
Instead of this:
wb.save(dest / filename.name)
You want this:
wb.save(dest / f'{filename.stem}_addtext{filename.suffix}')
filename.stem
gets you the .name
without the suffix (i.e. '.xlsx')