我希望将现有的excel文件作为不同的工作表收集到一个excel文件中,我的代码如下所示
import pandas as pd
from openpyxl import load_workbook
import numpy
path = r'/home/shaumne/Desktop/ds/deneme.xlsx'
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine="openpyxl")
writer.book = book
f1 = r'/home/shaumne/Desktop/ds/SP.xlsx'
df = pd.DataFrame(eval(f1))
f2 = r'/home/shaumne/Desktop/ds/SB.xlsx'
df2 = pd.DataFrame(eval(f2))
f3 = r'/home/shaumne/Desktop/ds/SD.xlsx'
df3 = pd.DataFrame(eval(f3))
df.to_excel(writer, sheet_name="SP")
df2.to_excel(writer, sheet_name="SB")
df3.to_excel(writer, sheet_name="SD")
writer.close()
但我得到“文件不是zip文件”错误
Traceback (most recent call last):
File "/home/shaumne/Desktop/ds/ds.py", line 7, in <module>
book = load_workbook(path)
File "/home/shaumne/.local/lib/python3.10/site-packages/openpyxl/reader/excel.py", line 315, in load_workbook
reader = ExcelReader(filename, read_only, keep_vba,
File "/home/shaumne/.local/lib/python3.10/site-packages/openpyxl/reader/excel.py", line 124, in __init__
self.archive = _validate_archive(fn)
File "/home/shaumne/.local/lib/python3.10/site-packages/openpyxl/reader/excel.py", line 96, in _validate_archive
archive = ZipFile(filename, 'r')
File "/usr/lib/python3.10/zipfile.py", line 1267, in __init__
self._RealGetContents()
File "/usr/lib/python3.10/zipfile.py", line 1334, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
1条答案
按热度按时间cczfrluj1#
没关系,我会回答我的问题,哈哈哈哈。
下面是我找到的解决方案: