5

Trying to open xlsm file using python

Below is the code :

import libraries
import openpyxl
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.styles import colors
from openpyxl.styles import Color, PatternFill, Font, Border

#path of the source sheet
path = "C:\DATA\PYTHON\Practise\SysTSAutSW300PFCRebuildDemo.xlsm"
wb1 = load_workbook(path)

sheet = wb1.get_sheet_by_name('PFC_Rebuild')

celldata = sheet['L33']

print celldata

It gives the below error :

Warning (from warnings module): File "C:\Python27\lib\site-packages\openpyxl\worksheet\header_footer.py", line 49 warn("""Cannot parse header or footer so it will be ignored""") UserWarning: Cannot parse header or footer so it will be ignored

stovfl
  • 14,998
  • 7
  • 24
  • 51
Sam
  • 61
  • 1
  • 1
  • 7
  • 2
    It's not an error, it's a warning. It's telling you that `openpyxl` lacks support for some of the features of the file you are trying to open. Whether that matters or not depends on what you are trying to do and whether you consider headers and footers important. Remember, `openpyxl` is a volunteer open-source project. The only software that can be guaranteed to do *everything* that Excel does is Excel itself. – BoarGules Jan 09 '19 at 09:31
  • Please don't put code in comments: it's impossible to read. If you want to amend the code in the question, edit the question. – BoarGules Jan 09 '19 at 09:37
  • Yup, Thanks Got it. Sorry about the code in comments – Sam Jan 09 '19 at 09:37
  • Relevant [python-openpyxl-excel-file-reading-error](https://stackoverflow.com/questions/31583401/python-openpyxl-excel-file-reading-error) – stovfl Jan 09 '19 at 13:29
  • 1
    @BoarGules I face the same problem: it's a warning but the consequence is, that I cannot load a workbook correctly. – aurumpurum Mar 04 '22 at 14:50
  • @aurumpurum Then maybe try making a copy of the workbook, that leaves out the feature complained of, and have your program load the simplified version instead. If that isn't a solution, then consider proposing a feature request to the `openpyxl` project. – BoarGules Mar 04 '22 at 16:17

3 Answers3

7

"""Cannot parse header or footer so it will be ignored"""

The warning occurred because you have an header or footer on your excel file that can't be parsed by openpyxl. If you want to get rid of the warning, you can remove the header and footer from the excel file with

File -> Info -> Check for Issues -> Inspect Document -> Remove your header and footer.

realr
  • 3,652
  • 6
  • 23
  • 34
Mang Hartono
  • 148
  • 3
  • 7
4

To get rid of this warning, change your load_workbook call to this:

wb1 = load_workbook(filename=path, read_only=True)
Elijan9
  • 1,269
  • 2
  • 17
  • 18
  • This works, but ... you can't then get at the value of cells, it seems (you certainly can't use the offset function). – Andy Brown Sep 14 '21 at 15:03
1

You can influence the UserWarings using the warnings module. Also see Ignore UserWarning from openpyxl using pandas

import warnings
warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
Stefan
  • 10,010
  • 7
  • 61
  • 117