I am trying to open a protected Excel file and copy the contents to another file I'm using this following snippet:
import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
filename='C:/Users/sh/Documents/Supply.xls'
xlwb = xlApp.Workbooks.Open(filename,False,True,None)
for sheet in xlwb.Worksheets:
xlApp = win32com.client.Dispatch("Excel.Application")
nwb = xlApp.Workbooks.Add()
sheet.Copy(Before=nwb.Workheets('Sheet1'))
nwb.SaveAs("C:/Users/sh/Documents/"+sheet.Name+'.xlsx') # Line 9
nwb.Close(True)
However, I'm not able to copy the contents as it throws an exception at
line number 9 saying 'Microsoft Excel Cannot Access the file at (line 9)
Is there any other method to copy contents of protected Excel workbook to another workbook in python?
import win32com.client
xlApp = win32com.client.Dispatch("Excel.Application")
filename='C:/Py/Input/Supply.xls'
xlwb = xlApp.Workbooks.Open(filename,False,True,None)
sheet= xlwb.Sheets(1)
shhet1=xlwb.Sheets(2)
nwb = xlApp.Workbooks.Add()
sheet.Copy(Before=nwb.Sheets(1))
nwb.SaveAs('Sheet1.csv',24)
nwb.Close(True)
nwb1 = xlApp.Workbooks.Add()
shhet1.Copy(Before=nwb1.Sheets(1))
nwb1.SaveAs('Sheet2.csv',24)
nwb1.Close(True)
Related
I am trying to import excel files and converting them into PDF using Python. I tried using win32com library but the saved file is not good. Saved PDF only contains first few columns from excel.
Code:-
import win32com.client
o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
wb_path = r'c:\user\desktop\sample.xls'
wb = o.Workbooks.Open(wb_path)
ws_index_list = [1,4,5] #say you want to print these sheets
path_to_pdf = r'C:\user\desktop\sample.pdf'
wb.WorkSheets(ws_index_list).Select()
wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf)
Using pandas with the openpyxl engine I want to open an excel woorkbook and add a sheet.
Because, the sheets are growing and getting quit big I want to create a new sheet without reading all the other sheet.
So instead of xlsxwriter, I did start using openpyxl, however I get the following issue that the file gets damaged and needs to recovered by excel.
After that, when I run it a second time, I get the python error that the excel file is 'raise BadZipFile("File is not a zip file")'.
This my test code:
import openpyxl
import pandas
from pandas import ExcelWriter
from pandas import ExcelFile
from openpyxl import Workbook
from openpyxl import load_workbook
sheet1 = 'sheet1'
sheet2 = 'sheet2'
if os.path.exists(filename):
workbook = openpyxl.load_workbook(filename)
else:
workbook = Workbook()
writer = pandas.ExcelWriter(filename, engine='openpyxl')
writer.book = workbook
df_1.to_excel(writer, sheet_name=sheet1)
writer.save()
writer.close()
time.sleep(2) # to give it some time. But doesn't help :(
if os.path.exists(filename):
workbook = openpyxl.load_workbook(filename)
else:
workbook = Workbook()
writer = pandas.ExcelWriter(filename, engine='openpyxl')
writer.book = workbook
df_2.to_excel(writer, sheet_name=sheet2)
writer.save()
writer.close()
Any suggestions how to solve this? Or do I miss something?
btw, excel 365 - 16.46 macOS
here is my code:
import openpyxl as op
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl import worksheet
path = ...
op.load_workbook(path)
wb = Workbook()
ws2 = wb.create_sheet('Sheet2')
wb.save(filename = 'NameOfTheFile.xlsx')
Now everything works, except saving the file, when I add:
print(wb.sheetnames)
it does print out the Sheet2 I just added, but for some reason when I go to the documents where the file is, it does not update it, that sheet is nowhere to be found.
I am trying to open and format a specific Excel worksheet. However I am having trouble trying to find out how to look at a specific worksheet.
The code I'm trying to use to open the workbook and go to a specific worksheet and then change the font for specific cells is:
from openpyxl import Workbook
def applyValidations(path,tabname):
workbook = Workbook(path)
worksheet = workbook[tabname]
c = worksheet['A1:A5']
c.font = Font(size=22)
The error I'm getting is:
KeyError: 'Worksheet Department Data does not exist.'
Department Data is the name of the worksheet which does exist in the workbook.
Here's some code I use regularly with openpyxl, which may solve your Q:
from openpyxl import load_workbook
wb = load_workbook(filename=data_file, read_only=True)
ws = wb.active
print(wb.sheetnames)
I want to do something like this:
import openpyxl as x
wb = x.load_workbook(filename)
# do some edit to the workbook
wb.save(filename)
The file specified by the filename is opening in Excel. Excel is locking the file so I will get permission denied error running the above code. Is there a way to edit/save it?
from openpyxl import load_workbook
ifile = 'Whales.xlsx'
wb = load_workbook(filename=ifile)
# do some edit to the workbook
wb.save(ifile)