I
I
Igor Raskin2019-04-07 21:53:10
Python
Igor Raskin, 2019-04-07 21:53:10

How to set relative path for win32com excel object in python?

Now the code works, but in the workbook.SaveAs save line, the path is written manually. How can I correctly write the new_file_path variable there instead?

# -*- coding: utf-8 -*-
import win32com.client as win32
import os
excel = win32.Dispatch("Excel.Application")
workbook = excel.Workbooks.Add()
print('test')
print(os.path.dirname(__file__))
new_file_path = os.path.dirname(__file__) + "/UpdatedSheet.xls"
print(new_file_path)
workbook.SaveAs("D:\\Мои документы\\PyCharm\\Project_1\\Excel\\UpdatedSheet.xls")
workbook.close

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2019-04-07
@shinma

workbook.SaveAs(new_file_path)
just deal with the path from the root, cv. os.path methods
like this

CURR_DIR = os.path.dirname(os.path.realpath(__file__))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question