0

my last part of code is as follows;

from openpyxl import Workbook

import win32com.client as win32

excel = win32.gencache.EnsureDispatch('Excel.Application')

for wb in excel.Workbooks:

    if wb.Name[:10] == 'da_woprint' :

        print('yes')

        import os

        os.chdir('C:\\Users\\maliadil\\Desktop')

        wb.SaveAs(r'C:\Users\maliadil\Desktop\{0}.xls'.format(r))



        excel.Application.Quit()

browserchr3.quit()

I have automated my chrome to use one of my Maximo web application to do some task and get the daily report which comes out in excel (.xls)format.I was able to do the whole thing,except the last step,which is nothing but saving this excel file,in a directory using a variable "r"(which contain previous day's date in string format),in which I am getting the error.But I am able to save it with a static name`.

  • You should update your question to a real question. Add some context and a more sophisticate example – Nebulosar May 09 '18 at 12:38
  • @Nebulosar:Here I am just handling an excel workbook,which I want to save it using the string name, in a variable called ''r".But I have only the knowledge to store the file with static name as given above as "5.xls",which I wish to have with variable,like r+".xls".I would HIGHLY APPRECIATE if you could help me. – ADIL HASSAN May 10 '18 at 15:52
  • can anyone help me please – ADIL HASSAN May 11 '18 at 16:30

1 Answers1

0

Judging from your comment:

What I think you want is that the 5 is replaced with a variable. In your case you want it to be stored in variable r. (This is not a very good name by the way).

So what you need to do is to inject your variable into your string somehow. There are a couple of ways to do this.

The first way is my favorite:

r = 5
wb.SaveAs('C:\Users\maliadil\Desktop\{0}.xls'.format(r))

You could also use %s to make this case work and a couple of other things, for more info, here

If you want to store the whole string, what you may want to do, I still don't really understand the case, is the following:

r = 'C:\Users\maliadil\Desktop\{0}.xls'.format(5)
wb.SaveAs(r)
Nebulosar
  • 1,727
  • 3
  • 20
  • 46
  • @Nebulsour,Thanks for your support,I corrected the code with following line as you told "wb.SaveAs(r'C:\Users\maliadil\Desktop\{0}.xls'.format(r))"but still getting error pywintypes.com_error: Make sure the specified folder exists. \n• Make sure the folder that contains the file is not read-only.\n• Make sure the file name does not contain any of the following characters: < > ? [ ] : | or *\n• Make sure the file/path name doesn't contain more than 218 characters.", 'xlmain11.chm', 0, -2146827284), None) – ADIL HASSAN May 14 '18 at 05:28
  • @ADILHASSAN You could add it to your question so that someone can respond to it? Did this fix yoir initial issie though? – Nebulosar May 14 '18 at 05:34
  • ,no it did not fix my issue,the above said error is coming whenvere I am trying to save file with variable,otherwise saving the file statically is happening – ADIL HASSAN May 14 '18 at 05:38
  • @ADILHASSAN please update your question by editing it, there is a button for it just below your question, update your code, tell us what is not working , include your error and ask a direct question, this way we are more likely to find you a fit anwser – Nebulosar May 14 '18 at 05:42
  • ,I finally found out the issue, it was because my variable "r" was a date thus its value was coming in the format,2018-01-01 00:00:00.Now our file name will not support ":",So I used string slicing [:10] to get only the date part as variable "r" Thanks Four your support. – ADIL HASSAN May 14 '18 at 12:41
  • Glad you found your answer – Nebulosar May 14 '18 at 12:56