datemode error using xlutils - python

I am trying to modify Excel files using Python, but I can't get the xlutils package to work correctly. When I try an example (from this thread):
from xlutils.copy import copy
w = copy('book1.xls')
w.get_sheet(0).write(0,0,"foo")
w.save('book2.xls')
I get the following result:
Traceback (most recent call last):
File "names3.py", line 2, in <module>
w = copy('names.xls')
File "C:\Program Files (x86)\Python27\lib\site-package
g\xlutils\copy.py", line 19, in copy
w
File "C:\Program Files (x86)\Python27\lib\site-package
g\xlutils\filter.py", line 937, in process
reader(chain[0])
File "C:\Program Files (x86)\Python27\lib\site-package
g\xlutils\filter.py", line 61, in __call__
filter.workbook(workbook,filename)
File "C:\Program Files (x86)\Python27\lib\site-package
g\xlutils\filter.py", line 287, in workbook
self.wtbook.dates_1904 = rdbook.datemode
AttributeError: 'str' object has no attribute 'datemode'
I can barely find any information about this error, I would really appreciate any help!
Thanks

xlutils.copy works on a xlrd.Book instance. You need to create such an instance first. This works:
from xlrd import open_workbook
from xlutils.copy import copy
wb = open_workbook('book1.xls')
wb_copy = copy(wb)
wb_copy.get_sheet(0).write(0,0,"foo")
wb_copy.save('book2.xls')

Related

xlrd assertion error when opening a .xls file (converted from .xlsx): assert _unused_i == nstrings - 1

I have a script that uses the xlrd library to read and write .xls files. The program works for most .xls', but I found that after I converted an .xlsx to .xls and try to open the workbook, I get the following assertion error:
Traceback (most recent call last):
File "/home/troublebucket/Projects/script.py", line 51, in <module>
wb = xlrd.open_workbook(bom_table_wb)
File "/home/troublebucket/.local/lib/python3.10/site-packages/xlrd/__init__.py", line 172, in open_workbook
bk = open_workbook_xls(
File "/home/troublebucket/.local/lib/python3.10/site-packages/xlrd/book.py", line 104, in open_workbook_xls
bk.parse_globals()
File "/home/troublebucket/.local/lib/python3.10/site-packages/xlrd/book.py", line 1211, in parse_globals
self.handle_sst(data)
File "/home/troublebucket/.local/lib/python3.10/site-packages/xlrd/book.py", line 1178, in handle_sst
self._sharedstrings, rt_runlist = unpack_SST_table(strlist, uniquestrings)
File "/home/troublebucket/.local/lib/python3.10/site-packages/xlrd/book.py", line 1472, in unpack_SST_table
assert _unused_i == nstrings - 1
AssertionError
I tried commenting out the assertion in book.py's unpack_SST_table(), and the script ran without errors but didn't actually read from the workbook. Any advice would be appreciated!

Creating and using Excel with multiple sheets in Python

I am creating an Excel file with two sheets from 2 different data frames. Here is a sample code:
import pandas as pd
import openpyxl
import xlsxwriter
with pd.ExcelWriter('output.xlsx',engine='xlsxwriter') as wr:
df1.to_excel(wr, sheet_name='Final',index=False)
df2.to_excel(wr, sheet_name='Tie_Line_Data',index=False)
When I run it in any Python IDE, it runs perfectly. However, when I run it in Terminal, it gives the following error:
Traceback (most recent call last):
File "/home/memiit/public_html/Pooja_MemiIT_Server/weather_demand_data_automated_mail.py", line 382, in <module>
with pd.ExcelWriter('output.xlsx',engine='xlsxwriter') as wr:
File "/home/memiit/public_html/python3/lib/python3.6/site-packages/pandas/io/excel/_xlsxwriter.py", line 187, in __init__
self.book = xlsxwriter.Workbook(path, **engine_kwargs)
AttributeError: module 'xlsxwriter' has no attribute 'Workbook'
How do I solve this, or is there any alternative way of doing it.

How to open and close an excel workbook in a python for loop using xlwings

I need to open and close the same workbook in a python for loop without necessarily saving the workbook. I tried the following in xlwings:
import xlwings as xw
for i in range(5):
print(i)
book = xw.Book()
book.app.quit()
However this will only run for the first iteration. At the second iteration I get an error:
Traceback (most recent call last):
File "D:/TEMP/SE/python/ref.py", line 5, in <module>
book = xw.Book('tree_template.xlsx')
File "C:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\SE\lib\site-packages\xlwings\main.py", line 533, in __init__
for wb in app.books:
File "C:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\SE\lib\site-packages\xlwings\main.py", line 374, in books
return Books(impl=self.impl.books)
File "C:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\SE\lib\site-packages\xlwings\_xlwindows.py", line 397, in books
return Books(xl=self.xl.Workbooks)
File "C:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\SE\lib\site-packages\xlwings\_xlwindows.py", line 313, in xl
self._xl = get_xl_app_from_hwnd(self._hwnd)
File "C:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\SE\lib\site-packages\xlwings\_xlwindows.py", line 222, in get_xl_app_from_hwnd
ptr = accessible_object_from_window(child_hwnd)
File "C:\Users\aboufira\AppData\Local\Continuum\miniconda3\envs\SE\lib\site-packages\xlwings\_xlwindows.py", line 190, in accessible_object_from_window
res = oledll.oleacc.AccessibleObjectFromWindow(
File "_ctypes/callproc.c", line 948, in GetResult
OSError: [WinError -2147467259] Unspecified error
Why does this occur? How can I get xlwings to exit out of the application without causing any problems?
Your code was almost correct, use:
import xlwings as xw
for i in range(5):
print(i)
app = xw.App(visible=True)
book = xw.Book()
app.quit()
You can also use app = xw.App(visible=False) if you want to open a workbook without showing it.
Use this to suffice the pop-ups
app.display_alerts=False

Tablib xlsx file badZip file issue

I am getting error on opening xlsx extension file in windows 8 using tablib library.
python version - 2.7.14
error is as follows:
python suit_simple_sheet_product.py
Traceback (most recent call last):
File "suit_simple_sheet_product.py", line 19, in <module>
data = tablib.Dataset().load(open(BASE_PATH).read())
File "C:\Python27\lib\site-packages\tablib\core.py", line 446, in load
format = detect_format(in_stream)
File "C:\Python27\lib\site-packages\tablib\core.py", line 1157, in detect_format
if fmt.detect(stream):
File "C:\Python27\lib\site-packages\tablib\formats\_xls.py", line 25, in detect
xlrd.open_workbook(file_contents=stream)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 120, in open_workbook
zf = zipfile.ZipFile(timemachine.BYTES_IO(file_contents))
File "C:\Python27\lib\zipfile.py", line 770, in __init__
self._RealGetContents()
File "C:\Python27\lib\zipfile.py", line 811, in _RealGetContents
raise BadZipfile, "File is not a zip file"
zipfile.BadZipfile: File is not a zip file
path location is as follows =
BASE_PATH = 'C:\Users\anju\Downloads\automate\catalog-5090 fabric detail and price list.xlsx'
Excel .xlsx files are actually zip files. In order for the unzip to work correctly, the file must be opened in binary mode, as such your need to open the file using:
import tablib
BASE_PATH = r'c:\my folder\my_test.xlsx'
data = tablib.Dataset().load(open(BASE_PATH, 'rb').read())
print data
Add r before your string to stop Python from trying to interpret the backslash characters in your path.

TypeError when trying to open a workbook using openpyxl

I'm trying to use openpyxl to open and modify an existing excel workbook, but I can't even open the file without getting an error.
from openpyxl import load_workbook
ws = load_workbook('PO-Copy.xlsx')
I get a long TypeError as a result:
Traceback (most recent call last):
File "<module1>", line 6, in <module>
File "C:\Python27\Lib\site-packages\openpyxl\reader\excel.py", line 151, in load_workbook
_load_workbook(wb, archive, filename, read_only, keep_vba)
File "C:\Python27\Lib\site-packages\openpyxl\reader\excel.py", line 224, in _load_workbook
keep_vba=keep_vba)
File "C:\Python27\Lib\site-packages\openpyxl\reader\worksheet.py", line 308, in read_worksheet
fast_parse(ws, xml_source, shared_strings, style_table, color_index)
File "C:\Python27\Lib\site-packages\openpyxl\reader\worksheet.py", line 296, in fast_parse
parser.parse()
File "C:\Python27\Lib\site-packages\openpyxl\reader\worksheet.py", line 84, in parse
dispatcher[tag_name](element)
File "C:\Python27\Lib\site-packages\openpyxl\reader\worksheet.py", line 282, in parse_data_validation
dv = parser(tag)
File "C:\Python27\Lib\site-packages\openpyxl\worksheet\datavalidation.py", line 179, in parser
dv = DataValidation(**element.attrib)
TypeError: __init__() got an unexpected keyword argument 'errorStyle'
Has anyone else ran into this error? is there a fix I can use to keep going?
The ability to read DataValidation in existing files was added in openpyxl 2.1 but was limited to what DataValidation in Python supported. Work has started on supporting DataValidation fully and is available in the 2.2 branch at https://bitbucket.org/habub68/openpyxl

Categories

Resources