I have file path problem when loading an Excel file using openpyxl.
>>>openpyxl.__version__
'2.5.12'
>>>jupyter.__version__
'5.7.4'
import os
import openpyxl
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.reader.excel import load_workbook
>>>os.getcwd()
'/Users/barbara/Documents/17_BMO'
path = "Users/barbara/Documents/17_BMO/Region.xlsx"
wb1 = openpyxl.load_workbook(path)
Instead of loading the workbook, I instead receive the error message:
FileNotFoundError: [Errno 2] No such file or directory: 'Users/barbara/Documents/17_BMO/Region.xlsx'
I fixed it.
I have two same excel files saved in Desktop and Documents. I firstly pointed workbook's path to Desktop, somehow, I can't load excel in Python, then I changed the path to Documents, it didn't work neither. Later on, I changed back to Desktop as my path, it worked. Don't why.
Related
I use Openpyxl in Python to write some data in an Excel file using Workbook.
When I want to save the workbook to a file, I can only provide a filename argument, so I can not write files in other directories.
here is my sample code:
import openpyxl
file_name = "sample.xlsx"
wb = openpyxl.Workbook()
# writing some data to workbook
wb.save(filename=file_name)
I have checked the documentation via this link and found nothing more.
Python: 3.10.7
Openpyxl: 3.0.10
Can you help me provide workarounds to solve this problem?
You can indicate a full path + filename, and it will create a file where you need it:
wb = Workbook()
wb.save(r'D:\folder\folder\folder\Filename.xlsx')
I am trying to autofit the columns in my python generated xlsx file.
Found the code from here[https://stackoverflow.com/a/33665967/5518944], but getting an exception.
I am using Microsoft Office 2015.
Using this code:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
ends up in following error:
[...]Python36\lib\site-packages\win32com\client\gencache.py", line 236, in GetModuleForCLSID
__import__(sub_mod_name)
ModuleNotFoundError: No module named 'win32com.gen_py.00020813-0000-0000-C000-000000000046x0x1x8._Application'
Are you able to help me with this problem?
I can access and edit .xlsx files using:
from win32com.client import Dispatch
xl = Dispatch("Excel.Application")
wb = xl.Workbooks.Open(Filename="yourfile.xlsx")
ws = wb.Worksheets(1)
etc..
But i'm not sure if you really need EnsureDispatch, see this for mor about the differences.
I am trying to write a script that live edits an open excel file, but when I try to run the script that uses openpyxl and reads from a cell then writes data back to that cell with an edit, it gives this error PermissionError: [Errno 13] Permission denied: 'GameExcel.xlsx' is there a way around this using another module, or is there a secret I am missing
Edit here's the code, also this is just me learning before I integrate it into the full code.
import openpyxl
from openpyxl import load_workbook
from openpyxl import workbook
from openpyxl.utils import get_column_letter
import os
import tkinter as tk
from tkinter import messagebox as tkMsgBox
import time
os.chdir("D:\Scripts\Python\Testing Scripts\My Excel Game")
wb = load_workbook("GameExcel.xlsx")
names = wb.sheetnames
sheet = wb['GameEnviroment']
#userInput = (input("what would you like it to say?"))
#print(userInput)
C3Val = sheet['C4'].value
sheet.cell(row=3, column=4).value = (C3Val + ' 4')
wb.save('GameExcel.xlsx')
print(C3Val + ' 3')
#sheet['A1']=userInput
This is due to the operating system limitation (ie Windows). It has nothing to do with openpyxl, Python or even Excel. POSIX based OS do not have such a limitation.
The answer to this question ("How to bypass permissions to write to an open file") is simply "You can't".
The option that I went with, and this only works with excel open, is xlwings
I have used openpyxl with Workbook(write_only=True) to create large Excel xlsx files. In this mode I cannot format the Excel headers. I then save the xlsx and open it again with openpyxl load_workbook(my_book) and have the ability to format the cells. I then save the file. If the file isn't too large it saves but otherwise gives a memory error.
So Openfile allows me to create and save the worksheet but not necessarily re-open and save the same worksheet.
In this example I just load_workbook then save without changing the xlsx to show the error
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Font
from openpyxl.styles import PatternFill
wb = Workbook(write_only=True)
# then append a lot of rows
logging.info('Save unformatted xlsx')
wb.save(my_book)
workbook = load_workbook(my_book)
# the worksheet always loads ok at this point, even when 700,000 rows
workbook.save(my_book)
## Immediately after load_workbook I do workbook.save(my_book).
When the rowcount is around 8600 there is no problem. When 350,000 there is a memory error
File "src\lxml\serializer.pxi", line 1268, in lxml.etree._IncrementalFileWriter._handle_error
File "src\lxml\etree.pyx", line 316, in lxml.etree._ExceptionContext._raise_if_stored
File "src\lxml\serializer.pxi", line 650, in lxml.etree._FilelikeWriter.write
MemoryError
logging.info('Saved unformatted xlsx immediately after opening again')
workbook = load_workbook(my_book)
# If no error I do some formatting and all is well and can save ok
Python 3.4.3
openpyxl (2.5.1)
lxml (4.2.1)
There are a lot of solutions for older versions but I cannot see any for openpyxl (2.5.1).
Does anyone have an answer for openpyxl, or can recommend what to use to open an existing large xlsx and format cells?
It wasn't of topic, it was an error even if just a memory error. Anyway in the end I just exported to CSV rather than XSLX as Excel still opens it nicely, not worth the bother.
I am trying to copy an image from an excel named Inputs_v3 and sheet named Inputs and save. The code is as follows`
import win32com.client as win32
from PIL import ImageGrab
from xlrd import open_workbook
import os
excel = win32.gencache.EnsureDispatch("Excel.Application")
wb = open_workbook('Inputs_v3.xlsm')
r = wb.sheet_by_name('Inputs')
r.CopyPicture()
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')
` The error is as follows
'Attribute error: 'Sheet' object has no attribute 'CopyPicture''
Please suggest where I am doing wrong.Thanks in advance
Use a python library called excel2img. In one line you can take a screenshot from any excel file
import excel2img
excel2img.export_img("Excel File Full Path", "Target Image full Path", "Excel SheetName", None)
and you can identify a specific cells range as well.
import excel2img
excel2img.export_img("test.xlsx", "test.bmp", "", "Sheet2!B2:C15")
I hope this will help.
The following code will get you the win32com reference that you actually need to access the Excel worksheet's objects and methods:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open('myworkbook.xlsx')
ws = wb.Worksheets('worksheet_name') # alternatively Worksheets(1) etc
Now you can do, for example:
ws.Shapes(1).CopyPicture()
I've tested this with Python 3.4, pywin32 219 and Excel 2010 on Windows 7.
Note that this doesn't involve xlrd at all - that's a package that can read Excel files without having Excel installed on the computer, but I don't know if it supports getting images of or from Excel workbooks.