0

我正在尝试将多个 .xls 文件合并到一个工作簿中,其中每个文件都插入到一个以 .xls 文件名命名的工作表中。

在网上冲浪时,我看到了 Pyexcel 的文档和一个特定的模块,如此处所写,可以轻松完成这项工作。这是代码。

from pyexcel.cookbook import merge_all_to_a_book
import glob 
merge_all_to_a_book(glob.glob("Dir\*.xls"),"output.xls")

正如预期的那样,它不起作用。这是控制台输出。

File "..\Desktop\scripts\provaimport.py", line 48, in <module>


merge_all_to_a_book(glob.glob("C:\Users\Tesisti\Desktop\forpythonscript\*.xls"),"output.xls")
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel\cookbook.py", line 148, in merge_all_to_a_book
    merged.save_as(outfilename)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel\internal\meta.py", line 339, in save_as
    return save_book(self, file_name=filename, **keywords)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel\internal\core.py", line 51, in save_book
    return _save_any(a_source, book)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel\internal\core.py", line 55, in _save_any
    a_source.write_data(instance)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel\plugins\sources\file_output.py", line 38, in write_data
    **self._keywords)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel\plugins\renderers\excel.py", line 30, in render_book_to_file
    save_data(file_name, book.to_dict(), **keywords)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel_io\io.py", line 119, in save_data
    **keywords)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel_io\io.py", line 141, in store_data
    writer.write(data)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel_io\book.py", line 58, in __exit__
    self.close()
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\pyexcel_xls\xlsw.py", line 86, in close
    self.work_book.save(self._file_alike_object)
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\xlwt\Workbook.py", line 710, in save
    doc.save(filename_or_stream, self.get_biff_data())
  File "C:\Users\Tesisti\Anaconda2\lib\site-packages\xlwt\Workbook.py", line 680, in get_biff_data
    self.__worksheets[self.__active_sheet].selected = True

关于如何修复的任何想法?

4

1 回答 1

0

在我看来, glob.glob("Dir*.xls") 返回了一个空的文件列表。因此 pyexcel 的插件 pyexcel-xls无法创建空文件

我建议当前的解决方案是采用最新的 pyexcel-xls 并在 merge_all_to_a_book 周围使用 try-except 语句,捕获空文件情况。

于 2018-03-21T19:40:31.760 回答