熊猫-Excel合并// ValueError:没有足够的值可解压

时间:2019-03-26 15:02:52

标签: python excel pandas valueerror

我想将.xlsx文件合并到一个.xlsx文档中,但保存到该文档中的各个工作表中。

我一直收到错误消息“ ValueError:没有足够的值要解压(预期2,得到1)”

通过故障排除,我读到这可能是因为.xlsx文件中存在合并的单元格。我放弃了令人讨厌的合并线路,但仍然没有运气。

number = 1
writer = pd.ExcelWriter("P&L Detail Combined.xlsx", engine = 'xlsxwriter')

for files in os.listdir(directory):
    if files.endswith(".xlsx"):
         df = pd.read_excel(files, index_col = 0)
         df = df.drop([-2,4])
         numstr = str(number)
         sheetnum = "GMCO Q" + numstr
         df.to_excel(writer, sheet_name = sheetnum, index=False)
         number =+ 1
         writer.save()

else:
    continue

理想的结果是将一个合并的文件与单独工作表中的数据合并在一起。

编辑::

Traceback (most recent call last):
  File "Combine_to_tabs.py", line 12, in <module>
    df = pd.read_excel(files, index_col = 0)
  File "/Applications/anaconda/lib/python3.6/site-packages/pandas/io/excel.py", line 200, in read_excel
io = ExcelFile(io, engine=engine)
  File "/Applications/anaconda/lib/python3.6/site-packages/pandas/io/excel.py", line 257, in __init__
self.book = xlrd.open_workbook(io)
  File "/Applications/anaconda/lib/python3.6/site-packages/xlrd/__init__.py", line 422, in open_workbook
ragged_rows=ragged_rows,
  File "/Applications/anaconda/lib/python3.6/site-packages/xlrd/xlsx.py", line 833, in open_workbook_2007_xml
x12sheet.process_stream(zflo, heading)
  File "/Applications/anaconda/lib/python3.6/site-packages/xlrd/xlsx.py", line 553, in own_process_stream
self.do_merge_cell(elem)
  File "/Applications/anaconda/lib/python3.6/site-packages/xlrd/xlsx.py", line 609, in do_merge_cell
first_cell_ref, last_cell_ref = ref.split(':')
ValueError: not enough values to unpack (expected 2, got 1)

0 个答案:

没有答案