I'm working with Excel files and I'm using the openpyxl package. I'm also making a GUI so I'm using the PyQt5 package. When I type something like this:
label = QtWidgets.QLabel("Text", window)
as soon as I type QtWidgets. I get options for what to do next, and I know what I have available.
I want to do the same with the openpyxl package, but it's not working. I have tried stuff like this:
wb = openpyxl.load_workbook('test.xlsx')
ws = wb['Sheet1']
for table in ws.tables:
print(table)
When I type ws., there's no "tables" option initially, unless I have already used it in my own code somewhere before. I want to know what options I have when I type in ws., but it's just not showing anything.
Examples:
It doesn't work with the openpyxl
It works with the PyQt5
Related
Using the openpyxl library, I am loading an Excel, adding values in it and I save it in another file.
It is working nicely.
Problem: when I use the library pyinstaller to bundle the application, the images aren't load and keep in the file anymore when the new file is saved.
PS :
I tried to let the images in the file, it doesn't work.
I tried to load the images with openpyxl, it doesn't work too.
import openpyxl
wb = openpyxl.load_workbook('input_file.xlsx')
ws = wb['excel_tab_name']
image = openpyxl.drawing.image.Image('my_image.jpg')
image.anchor = 'A1'
ws.add_image(image)
wb.save('output_file.xlsx')
The real problem is that it work when I use only openpyxl but when I bundle it, no image can stay or be loaded in the file.
I am ready to use another library to handle this problem if it is needed ! :)
Just try and catch the error.
If it's "you must install pillow to fetch image objects"
Use pip install image
And that's it !
I modified your code like this and it worked. Is this answering your question ?
import openpyxl
wb = openpyxl.load_workbook('input_path.xlsx')
ws = wb['excel_tab_name']
image = openpyxl.drawing.image.Image('my_image.jpg')
image.anchor = 'A1'
ws.add_image(image)
wb.save('output_path.xlsx')
I'm trying to create a Python script (I'm using Python 3.7.3 with UTF-8 encoding on Windows 10 64-bit with Microsoft Office 365) that exports user selected worksheets to PDF, after the user has selected the Excel-files.
The Excel-files contain a lot of different settings for page setup and each worksheet in each Excel-file has a different page setup.
The task is therefore that I need to read all current variables regarding page setup to be able to assign them to the related variables for export.
The problem is when I'm trying to get Excel to return the current print area of the worksheet, which I can't figure out.
As far as I understand I need to be able to read the current print area, to be able to set it for the export.
The Excel-files are a mixture of ".xlxs" and ".xlsm".
I've tried using all kind of different methods from the Excel VBA documentation, but nothing has worked so far e.g. by adding ".Range" and ".Address" etc.
I've also tried the ".UsedRange", but there is no significant difference in the cells that I can search for and I can't format them in a specific way so I can't use this.
I've also tried using the "IgnorePrintAreas = False" variable in the "ExportAsFixedFormat"-function, but that didn't work either.
#This is some of the script.
#I've left out irrelevant parts (dialogboxes etc.) just to make it shorter
#Import pywin32 and open Excel and selected workbook.
import win32com.client as win32
excel = win32.gencache.EnsureDispatch("Excel.Application")
excel.Visible = False
wb = excel.Workbooks.Open(wb_path)
#Select the 1st worksheet in the workbook
#This is just used for testing
wb.Sheets([1]).Select()
#This is the line I can't get to work
ps_prar = wb.ActiveSheet.PageSetup.PrintArea
#This is just used to test if I get the print area
print(ps_prar)
#This is exporting the selected worksheet to PDF
wb.Sheets([1]).Select()
wb.ActiveSheet.ExportAsFixedFormat(0, pdf_path, Quality = 0, IncludeDocProperties = True, IgnorePrintAreas = False, OpenAfterPublish = True)
#This closes the workbook and the Excel-file (although Excel sometimes still exists in Task Manager
wb.Close()
wb = None
excel.Quit()
excel = None
If I leave the code as above and try and open a test Excel-file (.xlxs) with a small PrintArea (A1:H8) the print function just gives me a blank line.
If I add something to .PrintArea (as mentioned above) I get 1 of 2 errors:
"TypeError: 'str' object is not callable".
or
"ps_prar = wb.ActiveSheet.PageSetup.PrintArea.Range
AttributeError: 'str' object has no attribute 'Range'"
I'm hoping someone can help me in this matter - thanks, in advance.
try
wb = excel.Workbooks.OpenXML(wb_path)
insead of
wb = excel.Workbooks.Open(wb_path)
My problem was with a german version of ms-office. It works now. Check here https://social.msdn.microsoft.com/Forums/de-DE/3dce9f06-2262-4e22-a8ff-5c0d83166e73/excel-api-interne-namen?forum=officede
I'm trying to add some icon sets to an existing excel file using python.
The excel file is written using xlsxwriter. As xlsxwriter does not support icon sets, I close the file, reopen it with openpyxl, add the icon sets and save it again. Problem is, that I loose all conditional formatting added previously. Opening the file in openpyxl with "keep_vba=True" results in a non-readable xlsx-File.
Any ideas how to achieve this?
Thanks in advance!
P.S.: Missed some details. Sorry for that. I write xlsx files in both cases (xlsxwriter and openpyxl) and use python 2.7 and the latest versions of openpyxl and xlsxwriter on a windows machine with excel 2013. Icon sets are little symbols like arrows (up, down) which can be used in conditional formatting.
OpenPyXl has a support for conditional formatting and Icon Sets.
See the official documentation: Conditional Formatting > IconSet
Here is an example:
>>> from openpyxl.formatting.rule import IconSet, FormatObject
>>> first = FormatObject(type='percent', val=0)
>>> second = FormatObject(type='percent', val=33)
>>> third = FormatObject(type='percent', val=67)
>>> iconset = IconSet(iconSet='3TrafficLights1', cfvo=[first, second, third], showValue=None, percent=None, reverse=None)
>>> # assign the icon set to a rule
>>> from openpyxl.formatting.rule import Rule
>>> rule = Rule(type='iconSet', iconSet=iconset)
Why do I receive this warning message every time I run my code? (below). Is it possible to get rid of it? If so, how do I do that?
My code:
from openpyxl import load_workbook
from openpyxl import Workbook
wb = load_workbook('NFL.xlsx', data_only = True)
ws = wb.active
sh = wb["Sheet1"]
ptsDiff = (sh['J127'].value)
print ptsDiff
The code works but I get this warning message:
Warning (from warnings module):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/openpyxl/reader/worksheet.py", line 320
warn(msg)
UserWarning: Unknown extension is not supported and will be removed
This error happens when openpyxl cannot understand/read an extension (source). Here is the list of built-in extensions openpyxl currently knows that is doesn't support:
Conditional Formatting
Data Validation
Sparkline Group
Slicer List
Protected Range
Ignored Error
Web Extension
Slicer List
Timeline Ref
Also see the Worksheet extension list specification.
Try to add single quotes to your data_only parameter like this:
wb = load_workbook('NFL.xlsx', data_only = **'True'**)
This works for me.
Using python 3.5 under Anaconda3, Excel 2016, Windows10 -- I had the same problem initially with an xlsx file. Tried to make it into a csv and did not work. What worked was: select the entire spreadsheet, copy on a Notepad, select the notepad text, paste in a new spreadsheet, save as xslx. It looks like any extra formatting would result in a warning.
It is already listed in the first answer what is wrong with it If you only want to get rid of the error that is given in red for some reason. You can go to the file location of the error and # the line where is says warn(msg) this will stop the error being displayed the code still works fine in my experience.I am not sure if this will work after compiled but this should work in the same machine.
PS:I had the same error and this is what I did because I though it could be confusing for the end user
PS:You can use a try and except error catcher too but this is quicker.
I am practicing with openpyxl and I'm working on an Excel file called 'test.xlsx'. The file only has 3 columns and 7 rows. The .xlsx file was created with LibreOffice.
When I run...
>>> #! python3
>>> import openpyxl
>>> wb = openpyxl.load_workbook('test.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet1')
>>> sheet.get_highest_column()
1025
The returned value should be 3.
A quick Google search suggested I run:
>>> sheet.calculate_dimension()
and got the return value:
'A1:AMK7'
This should only be 'A1:C7'.
I remember reading that LibreOffice could be part of the problem to this.
However, I can't switch to MSOffice, and I hate OpenOffice.
Is there suggestion on how I could fix this, or work around it?
Thanks!
It sounds like you're using older versions of LibreOffice and openpyxl. LibreOffice did used to set a default value of "A1:AMK7" for the dimensions but it version 5 doesn't seem to be doing that any more. openpyxl used to rely on the dimensions tag when reading files but hasn't done this for a while. Please try using openpyxl 2.3-b2