Python Forum
Saving as docx file from excel with new filename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving as docx file from excel with new filename
#1
Hi,

so I have a running invoice program that functions ok. But I want to tweak it to run better for me.

But having a couple of problems

Problem 1. I want to save as a docx file from the excel. It does this, but when I open the newly created docx file it says:

Word found unreadable content in demo1.docx. Do you want to recover contents of this doc?

All attempts to recover contents fail.It only has the new invoice number in it per program below ( this is a stripped down version of my program to try and solve the problems I want fixing ). So something I am trying to do causing this.

Problem 2. I want to save the file with the new invoice number as part of the filename. When I try the bottom 2 lines of the program below ( both#'ed at the moment ), with copyfile it does save but the new file is called +str(new_invoice_number)+.docx ie it doesn't insert the actual number of new_invoice_number.

With os.renames it errors with:

SyntaxError: EOL while scanning string literal, indicating the last closing parenthesis.

Any pointers?

Thank you

from openpyxl import load_workbook
import docx
import os
from shutil import copyfile

wb = load_workbook(r"C:\Users\Ron McMillan\Desktop\demo1.xlsx")  #opens the existing spreadsheet

sh = wb['Sheet']


invoice_number = sh['A4']   #takes existing invoice number
print (invoice_number.value)

new_invoice_number = invoice_number.value + 1   #increments existing invoice number
print(new_invoice_number)
sh['A4'] = new_invoice_number    #writes new invoice number into sheet



wb.save(r'C:\Users\Ron McMillan\Desktop\demo1.xlsx')   #saves workbook

wb.save(r'C:\Users\Ron McMillan\Desktop\demo1.docx')   #saves as docx



#copyfile(r'C:\Users\Ron McMillan\Desktop\demo1.xlsx',r'C:\Users\Ron McMillan\Desktop\+str(new_invoice_number)+.docx')


#os.renames(r'C:\Users\Ron McMillan\Desktop\demo1.xlsx',r'C:\Users\Ron McMillan\Desktop\'+ str(new_invoice_number) +'.xlsx')
Reply


Messages In This Thread
Saving as docx file from excel with new filename - by Chuck_Norwich - Sep-10-2019, 06:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 532 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 609 Feb-07-2024, 12:24 PM
Last Post: Viento
  no module named 'docx' when importing docx MaartenRo 1 1,330 Dec-31-2023, 11:21 AM
Last Post: deanhystad
  Search Excel File with a list of values huzzug 4 1,446 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 1,044 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Replace a text/word in docx file using Python Devan 4 4,500 Oct-17-2023, 06:03 PM
Last Post: Devan
  output provide the filename along with the input file processed. arjunaram 1 1,028 Apr-13-2023, 08:15 PM
Last Post: menator01
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,256 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,490 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 928 Feb-16-2023, 08:11 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020