Consolidation of excel files workbooks in a folder - python

I am a python beginner and I try to build a script which consolidates all existing excel workbooks into a new one in the same folder... However I have an error stating that the excel files could not be found.... I am stuck. see below the coding :
import win32com.client as win32
import os
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Add()
path = r"/Users/lb/Desktop/consolidation"
suffix = "xlsx"
dirs = os.listdir(path)
for file in dirs:
if file.endswith(suffix):
w = excel.Workbooks.Open(file)
w.Sheets(1).Copy(wb.Sheets(1))
wb.SaveAs(os.path.join(path, "result.xlsx"))
excel.Application.Quit()
This is the error when I run the script :
File "C:/Users/lb/PycharmProjects/New Project/fusion2.py", line 12, in
w = excel.Workbooks.Open(file)
File "C:\Users\lb\AppData\Local\Temp\gen_py\3.7\00020813-0000-0000-C000-000000000046x0x1x7\Workbooks.py", line 78, in Open
, Converter, AddToMru, Local, CorruptLoad)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "'business_Descriptor.xlsx' could not be found. Check the spelling of the file name, and verify that the file location is correct.
If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.", 'xlmain11.chm', 0, -2146827284), None)

I believe your path may be wrong. Usually speaking, the users directory in Windows should go something like this:
C:\\Users\\lb\\Desktop\\consolidation
If you are unsure about your paths, you can also debug your path by navigating through it in your python script until you find your folder with the os.chdir() command, and os.listdir() command. When you're at the folder you want, you can then use the os.getcwd() command.
I also recommend this reading if you want to get familiar with filesystems in python.
Continuing on from the previous part, it seems that in your code,
for file in dirs:
if file.endswith(suffix):
w = excel.Workbooks.Open(file)
w.Sheets(1).Copy(wb.Sheets(1))
You're trying to open the file with the string name of the file itself, not the path. If you take a look at the snippet below from this link you'll see that they append the name with the path directory. This should fix your issue :P
import os
for filename in os.listdir('path/to/dir'):
if filename.endswith('.log'):
with open(os.path.join('path/to/dir', filename)) as f:
content = f.read()
Essentially what you need to do is change your
w = excel.Workbooks.Open(file)->
w = excel.Workbooks.Open(os.path.join(path,file))
So the final result of that part of your code should look like this (Note the indentations at the if! Without them the if statement does nothing):
for file in dirs:
if file.endswith(suffix):
w = excel.Workbooks.Open(os.path.join(path,file))
w.Sheets(1).Copy(wb.Sheets(1))

Related

FileNotFoundError: [Errno 2] - Possible work network issue?

I am trying to read content of a file on my work network from my work network. I copy and pasted a code snippet from a google search and modified it to the below. Why might I still be getting [Errno 2] (I have changed some of the path names for this question board)
The file path in my file explorer shows that "> This PC > word common common" and I don't have "This PC" in my path. I tried adding that into the place I would think it goes in the string. That didn't solve it.
I tried making sure I have matching capitalization. That didn't solve it.
I tried renaming the file to have a *.txt on the end. That didn't solve it.
I tried the different variations of // and / and \ with and without the r predecessor and while that did eliminate the first error I was getting. It didn't help this error.
(Looking at the code errors in the right gutter is says my line length is greater than the PEP8 standard. While I doubt that is the root of my problem, if you can throw in the 'right' wrap method for a file path that long that would be helpful.)
myfile = open("z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246", "rt") # open lorem.txt for reading text
contents = myfile.read() # read the entire file into a string
myfile.close() # close the file
print(contents) # print contents
Full Error Copy:
C:\Users\e087680\PycharmProjects\FailureCompiling\venv\Scripts\python.exe C:/Users/e087680/PycharmProjects/FailureCompiling/FirstScriptAttempt.py
Traceback (most recent call last):
File "C:/Users/e087680/PycharmProjects/FailureCompiling/FirstScriptAttempt.py", line 1, in
myfile = open("z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246", "rt") # open lorem.txt for reading text
FileNotFoundError: [Errno 2] No such file or directory: 'z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246'
EDIT
DEBUG EFFORTS
working to figure out how to change directory. Just in case that is the problem. Tested this code bit
import os
path = "z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246"
os.chdir(path)
isExist = os.path.exists(path)
print(isExist)
Received this error
C:\Users\e087680\PycharmProjects\FailureCompiling\venv\Scripts\python.exe C:/Users/e087680/PycharmProjects/FailureCompiling/ScriptDebugJunkFile.py
Traceback (most recent call last):
File "C:/Users/e087680/PycharmProjects/FailureCompiling/ScriptDebugJunkFile.py", line 5, in <module>
os.chdir(path)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246'
My intention for adding the picture below is to show how File Explorer displays the file path for my file
FileExplorerPathScreenShot
EDIT
I think this confirms that my 'OS' doesn't have my file.
from os import path
path.exists("PCC_ESS_FC_Full_Report_65000122-1_R0016_962019_9246")
def main():
print ("File exists:" + str(path.exists('PCC_ESS_FC_Full_Report_65000122-1_R0016_962019_9246')))
if __name__== "__main__":
main()
Output
File exists: False
I thought OS was a standard variable for Operating system. Now I'm not sure.
EDIT
Using Cmd in DOS, I confirmed that my path for the z: is correct
EDIT - Success
I ran
import os
print( os.listdir("z:/"))
Confirmed I don't need the monster string of folders.
Confirmed, although explorer doesn't show it, it is a *.txt file
Once I implemented these two items the first code worked fine.
Thank you #Furas
To open and read a file specify the filename in your path:
myfile = open("U:/matrix_neo/word common common/hello world.txt", "rt") # open file
contents = myfile.read() # read the entire file into a string
myfile.close() # close the file
print(contents) # print contents
The U: is a mapped drive in my network.
I did not find any issue with your change dir example. I used a path on my U: path again and it returned True.
import os
path = "U:/matrix_neo/word common common"
os.chdir(path)
isExist = os.path.exists(path)
print(isExist)
The check the attributes on the directory that you are trying to read from. Also try to copy the file to a local drive for a test and see if you can read the file and also check if it exists.
This is an alternative to the above and uses your path to make sure that the long file path works:
import os
mypath = "z:/abcdefg/abc123_proj2/word_general/word common common/Users/Mariee/Python/abc_abc_ab_Full_Report_12345-1_R9999_962019_9246"
myfile = 'whatever is your filename.txt'
if not os.path.isdir(mypath):
os.makedirs (mypath)
file_path = os.path.join(mypath, myfile)
print(file_path)
if os.path.exists(file_path) is True:
with open(file_path) as filein:
contents = filein.read()
print(contents)
I tested this code using a long csv file.,Replace the variable myfile with whatever is your file name.

How to open an hdf5 not in library root

I can't open hdf5 files using h5py.File() function unless the files are in the root folder, and I was wondering if there's anyway to change this.
I've already been able to open up the files in the root folder this same way but as soon as a file goes inside of another folder within the root, it throws an error. I've also tried opening it with the path and it didn't work.
# string together file name for field data
file = 'magnets-l2_current-' + date_time
# import file
m = h5py.File(file, 'r')
OSError: Unable to open file (unable to open file: name = 'magnets-l2_current-20190506-102013.hdf5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)
This is the error it throws. Again, I have my venv folder as the root, and I can open any data files that are in that folder. But any files inside venv/Data/... I cannot open.

How can I iteratively modify excel filetypes using win32com and os?

I am trying to iterate over a directory that contains a number of xlsm (Macro-enabled excel) files, opening each file and saving as an xlsx file. Ideally, I would have three directories, one that holds my script, one where the unmodified files live, and another where the modified files will be saved.
The directory, macro_dir, contains: 'test1.xlsm', 'test2.xlsm', 'test3.xlsm'
I cannot get my code to work when I loop through each file in the directory. Code chunk A (below) works: In the excel.Workbooks.Open method, the file variable was an absolute path to one of the xlsm files and the wb.SaveAs method contained an absolute path with the new filename and extension.
A. Working code:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r'C:\Users\Documents\macro_dir\test1.xlsm')
excel.DisplayAlerts = False
wb.DoNotPromptForConvert = True
wb.CheckCompatibility = False
wb.SaveAs(r'C:\Users\Documents\macro_dir\output1.xlsx', FileFormat=51, ConflictResolution=2)
excel.Application.Quit()
B. My attempt at turning this into a loop:
import os
import win32com.client as win32
dir = r'C:\Users\Documents\macro_dir'
excel = win32.gencache.EnsureDispatch('Excel.Application')
for file in os.listdir(dir):
wb = excel.Workbooks.Open(file)
excel.DisplayAlerts = False
wb.DoNotPromptForConvert = True
wb.CheckCompatibility = False
wb.SaveAs(file[:-4] + 'xlsx', FileFormat=51, ConflictResolution=2)
excel.Application.Quit()
I expected code chunk B (above) to modify each .xlsm file and save them as .xlsx to the same directory. However, the code produces the following error:
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Sorry, we couldn't find test1.xlsm. Is it possible it was moved, renamed or deleted?", 'xlmain11.chm', 0, -2146827284), None)
EDIT: I cannot simply change the file extension, the file format must be converted by opening the file in excel and saving as an .xlsx
Its your for loop that is causing the error, not win32com. You aren't using the full path.
This should fix it.
for file in os.listdir(dir):
file = os.path.join(dir, file)
wb = excel.Workbooks.Open(file)
....

Python - utf-8 coding issue

My code is:
path = "d:\\path\\"
dirs = os.listdir(path)
print u'Files:'
for i in dirs:
if i[-3:] == 'xls':
print i
file = raw_input('File: ')
workbook = xlrd.open_workbook(file)
My filesnames contains letters as "ąń". workbook = xlrd.open_workbook(file) can't use file from raw_input. Filename is for example "mondayń.xls". I get error: No such file or directory: 'monday\xe4.xls'.print i command gives proper filenames. How can I solve it? I am sorry for my english...
P.S. I use python 2.7.10 and Win10
The files are in D:\\path, but you're only printing the file names, then opening the name they type. You need to join the name to the path, e.g. with fullpath = os.path.join(path, file), then open that.

Error 32, Python, file being used by another process

I have a simple program, which looks for all compressed folders in a directory, targets one compressed file, gets an excel file located inside the compressed file and moves it to another location (it does this for every excel file, for how many ever compressed folders):
path = 'C:\Users\me\Documents\Extract'
new_path = 'C:\Users\me\Documents\Test'
i = 0
for folder in os.listdir(path):
path_to_folder = os.path.join(path, folder)
zfile = zipfile.ZipFile(os.path.join(path, folder))
for name in zfile.namelist():
if name.endswith('.xls'):
new_name = str(i)+'_'+name
new_path = os.path.join(new_path, new_name)
zfile.close()
#os.rename(path_to_folde, new_path) -- ERROR HERE
shutil.move(path_to_folde, new_path) -- AND ERROR HERE
i += 1
I have tried 2 ways to move the excel file os.rename and shutil.move. I keep on getting an error:
WindowsError: [Error 32] The process cannot access the file beacause it is being used by another process.
I don't understand why this error persists, since I have closed every folder.
path = 'C:\Users\me\Documents\Extract'
destination_path = 'C:\Users\me\Documents\Test'
i = 0
for folder in os.listdir(path):
path_to_zip_file = os.path.join(path, folder)
zfile = zipfile.ZipFile(path_to_zip_file)
for name in zfile.namelist():
if name.endswith('.xls'):
new_name = str(i)+'_'+name
new_path = os.path.join(destination_path, new_name)
# This is obviously going to fail because we just opened it
shutil.move(path_to_zip_file, new_path)
i += 1
zfile.close()
Changed some of the variable names in your code snippet. Do you see your problem now? You're trying to move the zip file that your process has open. You'll need to copy the .xls file to your destination using the zipfile module.
If you are on a windows computer go to the task manager and hit the processes tab. Scroll down to anything that says python and end the process. You may have had python running with something else. Then try running your python program again and it should work.
downloaded files must be marked as 'unblock' in the properties window of the file
before they can be worked with code.

Categories

Resources