I've tried both

from xlutils.copy import copy
from xlutils import copy

both works. It seems your python lib is not in your lib path. try the following code:

import sys
print (sys.path)

and check if your lib path is there. if not add the lib path using:

sys.path.append('/your-lib-path')

in python code, or add the lib path to your environment variable PYTHONPATH

Answer from Yang on Stack Overflow
🌐
GitHub
github.com › python-excel › tutorial › blob › master › students › xlutils › copy.py
tutorial/students/xlutils/copy.py at master · python-excel/tutorial
from xlwt import easyxf · from xlutils.copy import copy · · rb = open_workbook('source.xls',formatting_info=True) rs = rb.sheet_by_index(0) wb = copy(rb) ws = wb.get_sheet(0) · plain = easyxf('') for i,cell in enumerate(rs.col(2)): if not i: continue ·
Author: python-excel
🌐
Stack Overflow
stackoverflow.com › questions › 35839326 › python3-xlutils-copy
python - Python3 xlutils.copy - Stack Overflow
March 7, 2016 - I use xlutils.copy() to copy an excel file I opened using xlrd. Then I proces the excel file, get the relevant data and add comments using xlwr.write() for the lines I used in the copied excel file. Last step is to save the copied excel file using xlwr.save(). So far, so good. The essentiel part of the code looks like this. import xlwt import xlrd from xlutils.copy import copy rb = xlrd.open_workbook(file) # print (rb) wb = copy(rb) # print (wb) sheet = rb.sheet_by_name(MNS Brandmelders') wsheet = wb.get_sheet(0) os.remove(file) os.chdir(path_opslag) # wb.save(file) os.chdir(path) continue
🌐
Stack Overflow
stackoverflow.com › questions › 21584834 › using-xlutils-xlwt-to-copy-workbook
python - Using xlutils xlwt to copy workbook - Stack Overflow
February 5, 2014 - from xlutils.copy import copy from xlrd import open_workbook from tempfile import TemporaryFile book = open_workbook("book.xlsm") book_copy = copy(book) book_copy.save("bookcopy.xlsm") book_copy.save(TemporaryFile())
Top answer
1 of 1
1

Perhaps you have multiple installations of Python, and the pip installed the xlutils in a different installation. If you try just:

import xlutils

I expect you'll get the same results as before. I feel this may be getting overlooked by some of the other posters. Your error message says it can't find the xlutils module, not some submodule or variable. Is this still the case?

When you have multiple installations of Python, only one is the "default", so to speak, and any module installations will install into that default Python installation. I am careful to only run two installations: Python 2.7 and 3.6. And I go into each installation and make sure that I have a pip2 and a python2, and a pip3 and python3, so that I can just reference directly to the Py version I want to use, either to run or to install new packages. Otherwise, if I just run pip, I'm not 1000% sure where it will go. (Okay, I'm exaggerating. I know Python 2 is my default. Just trying to make a point.) :)

Also, just because you think you only have one Python installation, that may not be the case. On Windows, yes. But on Mac, there is a preloaded Python installation, and updating the Python and getting new packages into the new version instead of the preloaded version can be tricky, depending on what you've done. Also, when you install an IDE like PyCharm, it wants to install a Python version, as well.

Last comment -- Sometimes (not usually but sometimes), an installed package will have a different name than you would think. That is not the case with xlutils. But, with fonttools, you install fonttools but you must import fontTools (note the capital 'T') into your Py script. Again, not the case with xlutils, but just be aware of this for the future.

🌐
Google Groups
groups.google.com › g › python-excel › c › xcmXXySlCEo
losing cell style when using xlutils.copy
> > When I am using the copy function from xlutils and save the workbook > > the image and the style are gone. > > How could I copy over the styles? Here is the code I use for copying > > ''' > > import xlrd, xlwt > > from xlutils.copy import copy > > > cb = xlrd.open_workbook('test.xls', on_demand=True) > > nb = copy(cb) > > # add some values to it > > nb.save('test_new.xls') > > ''' > > Losing styles: Read the xlrd docs on open_workbook().
Find elsewhere
Top answer
1 of 2
58

There are two parts to this.

First, you must enable the reading of formatting info when opening the source workbook. The copy operation will then copy the formatting over.

import xlrd
import xlutils.copy

inBook = xlrd.open_workbook('input.xls', formatting_info=True)
outBook = xlutils.copy.copy(inBook)

Secondly, you must deal with the fact that changing a cell value resets the formatting of that cell.

This is less pretty; I use the following hack where I manually copy the formatting index (xf_idx) over:

def _getOutCell(outSheet, colIndex, rowIndex):
    """ HACK: Extract the internal xlwt cell representation. """
    row = outSheet._Worksheet__rows.get(rowIndex)
    if not row: return None

    cell = row._Row__cells.get(colIndex)
    return cell

def setOutCell(outSheet, col, row, value):
    """ Change cell value without changing formatting. """
    # HACK to retain cell style.
    previousCell = _getOutCell(outSheet, col, row)
    # END HACK, PART I

    outSheet.write(row, col, value)

    # HACK, PART II
    if previousCell:
        newCell = _getOutCell(outSheet, col, row)
        if newCell:
            newCell.xf_idx = previousCell.xf_idx
    # END HACK

outSheet = outBook.get_sheet(0)
setOutCell(outSheet, 5, 5, 'Test')
outBook.save('output.xls')

This preserves almost all formatting. Cell comments are not copied, though.

2 of 2
12

Here's an example of usage of code that I'll propose as a patch against xlutils 1.4.1

# coding: ascii

import xlrd, xlwt

# Demonstration of copy2 patch for xlutils 1.4.1

# Context:
# xlutils.copy.copy(xlrd_workbook) -> xlwt_workbook
# copy2(xlrd_workbook) -> (xlwt_workbook, style_list)
# style_list is a conversion of xlrd_workbook.xf_list to xlwt-compatible styles

# Step 1: Create an input file for the demo
def create_input_file():
    wtbook = xlwt.Workbook()
    wtsheet = wtbook.add_sheet(u'First')
    colours = 'white black red green blue pink turquoise yellow'.split()
    fancy_styles = [xlwt.easyxf(
        'font: name Times New Roman, italic on;'
        'pattern: pattern solid, fore_colour %s;'
         % colour) for colour in colours]
    for rowx in xrange(8):
        wtsheet.write(rowx, 0, rowx)
        wtsheet.write(rowx, 1, colours[rowx], fancy_styles[rowx])
    wtbook.save('demo_copy2_in.xls')

# Step 2: Copy the file, changing data content
# ('pink' -> 'MAGENTA', 'turquoise' -> 'CYAN')
# without changing the formatting

from xlutils.filter import process,XLRDReader,XLWTWriter

# Patch: add this function to the end of xlutils/copy.py
def copy2(wb):
    w = XLWTWriter()
    process(
        XLRDReader(wb,'unknown.xls'),
        w
        )
    return w.output[0][1], w.style_list

def update_content():
    rdbook = xlrd.open_workbook('demo_copy2_in.xls', formatting_info=True)
    sheetx = 0
    rdsheet = rdbook.sheet_by_index(sheetx)
    wtbook, style_list = copy2(rdbook)
    wtsheet = wtbook.get_sheet(sheetx)
    fixups = [(5, 1, 'MAGENTA'), (6, 1, 'CYAN')]
    for rowx, colx, value in fixups:
        xf_index = rdsheet.cell_xf_index(rowx, colx)
        wtsheet.write(rowx, colx, value, style_list[xf_index])
    wtbook.save('demo_copy2_out.xls')

create_input_file()
update_content()
🌐
Read the Docs
app.readthedocs.org › projects › xlutils › downloads › pdf › latest pdf
xlutils Documentation Release 2.0.0 Simplistix Ltd May 03, 2018
May 3, 2018 - both of the xlrd and xlwt packages, they are collected together here, separate from either package. The utilities are · grouped into several modules within the package, each of them is documented below: xlutils.copy Tools for copying xlrd.Book objects to xlwt.Workbook objects.
🌐
ProgramCreek
programcreek.com › python › example › 107937 › xlutils.copy.copy
Python Examples of xlutils.copy.copy
def Write(self, sheet, row, data): '''更新写入''' if not isinstance(data, list): return rdxls = xlrd.open_workbook(self._fullname) wtxls = copy(rdxls) if sheet not in rdxls.sheet_names(): tables = wtxls.add_sheet(sheet) else: tables = wtxls.get_sheet(sheet) for i in range(len(data)): tables.write(row, i, data[i]) wtxls.save(self._fullname)
🌐
Google Groups
groups.google.com › g › python-excel › c › -GJ4h5AUAqg
Strange xlutils problem
January 27, 2021 - > OR > If using "import xlutilis" > Traceback (most recent call last): > File "C:\Python26\TT3.py", line 23, in <module> > tofile('C:\Python26\trash.xls', 'test1', 'testtrash.xls', '5') > File "C:\Python26\TT3.py", line 18, in tofile > w=xlutils.copy.copy(wb) > AttributeError: 'module' object has no attribute 'copy' ...and my guess here is that you've imported xlutils, but not copy: >>> import xlutils >>> xlutils.copy.copy · Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'module' object has no attribute 'copy' It's much better for to use: from xlutils.copy import copy
🌐
Iditect
iditect.com › faq › python › preserving-styles-using-python39s-xlrd-xlwt-and-xlutilscopy.html
Preserving styles using python's xlrd, xlwt, and xlutils.copy
"Preserving styles using xlutils.copy" Description: Explore how to retain styles when copying Excel files in Python using xlutils.copy library. from xlrd import open_workbook from xlutils.copy import copy # Open the Excel file rb = open_workbook('source.xls', formatting_info=True) wb = copy(rb) # ...
🌐
Stack Overflow
stackoverflow.com › questions › 51743275 › save-formatting-using-xlutils-copy
python - Save formatting using xlutils.copy - Stack Overflow
August 8, 2018 - I use this code to open a workbook create a copy,modify it and then save it. But i lose all the formatting, i cant't figure out how to save it. import xlrd import xlwt import datetime from xlutils.copy import copy #CREO WORKBOOK workbook_temp = xlrd.open_workbook('file1.xlsx') #workbook used to read the data first time and then to read an index i= int(workbook_temp.sheet_by_index(0).cell(0,0).value) #read the index print(i) workbook = xlrd.open_workbook('file'+str(i)+'.xlsx') #open the last workbook i= i+1 w= copy(workbook) w.get_sheet(0).write(0,0,i) #update the index n_fogli = workbook.nshee
🌐
PyPI
pypi.org › project › xlutils
xlutils · PyPI
This package provides a collection of utilities for working with Excel files. Since these utilities may require either or both of the xlrd and xlwt packages, they are collected together here, separate from either package. ... Tools for copying xlrd.Book objects to xlwt.Workbook objects.
🌐
Google Groups
groups.google.com › g › python-excel › c › 7cudSVVJoyo
problems with xlutils.copy
I made a test with xlutils.copy but I had problems: 1 - the model has a cell with a green background: the background in the copy is not the same tint of green of the original. 2 - when I write in a cell in the copy, the original cell style is lost. This is my source code: #!/usr/bin/env python from xlrd import open_workbook from xlutils.copy import copy wb = open_workbook('model.xls', formatting_info=1) cwb = copy(wb) sh = cwb.get_sheet(0) for r in range(10): sh.row(3+r).write(0, r+1) sh.row(3+r).write(1, "row %d" % (r+1)) cwb.save('output.xls') thank for your help
🌐
Nullege
nullege.com › codes › search › xlutils.copy.copy
Nullege
Ready to start coding? Python is the perfect language—simple, versatile, and used by top companies for web development, data science, and more.