我是python新手,也是该语言的初学者。我有以下问题,我正在尝试将html文件批量转换为excel文件。问题是我不知道如何将新的excel文件写入另一个文件夹,我最终将html文件重新编码为excel
from html2excel import ExcelParser
import os
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directory
print("Files in %r: %s" % (cwd, files))
os.chdir(r'C:\Users\Ahmed_Abdelmuniem\Desktop\Afternoon')
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directory
print("Files in %r: %s" % (cwd, files))
inputFolder = r'C:\Users\Ahmed_Abdelmuniem\Desktop\Afternoon' ## Change here the input folder
outputFolder = r'C:\Users\Ahmed_Abdelmuniem\Desktop\Evening' ## Change here the attachments output folder
for file in os.listdir(inputFolder):
if file.endswith(".html"):
parser = ExcelParser(file)
parser.to_excel(file)
错误日志:
Traceback (most recent call last):
File "C:\Users\Ahmed_Abdelmuniem\PycharmProjects\Batch HTML to Excel\main.py", line 19, in <module>
parser = ExcelParser(file)
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\html2excel\excel\parser.py", line 7, in __init__
super().__init__(file_path)
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\html2excel\base\parser.py", line 13, in __init__
self.load_workbook()
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\html2excel\excel\parser.py", line 30, in load_workbook
self._write_cell(i, j, col_data)
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\html2excel\base\parser.py", line 47, in _write_cell
self.ws.cell(row=row, column=col).value = val
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\cell\cell.py", line 215, in value
self._bind_value(value)
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\cell\cell.py", line 194, in _bind_value
value = self.check_string(value)
File "C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\lib\site-packages\openpyxl\cell\cell.py", line 162, in check_string
raise IllegalCharacterError
openpyxl.utils.exceptions.IllegalCharacterError
预期结果: 程序将“上午”文件夹中的每个html文件从html转换为excel,并将新的excel文件写入/保存到“晚上”文件夹中
我做了很多研究,但没有找到任何有效的方法
附言
以下代码适用于桌面中的单个文件,它是我尝试的代码的源代码
from html2excel import ExcelParser
input_file = r'C:\Users\Ahmed_Abdelmuniem\Desktop\088TE QTIF-794.html'
output_file = r'C:\Users\Ahmed_Abdelmuniem\Desktop\088TE QTIF-794.xlsx'
parser = ExcelParser(input_file)
parser.to_excel(output_file)
类似这样的东西应该可以进行一些小的调整:
我测试了此文件夹结构:
这是输出:
我显然无法测试代码的解析部分,因为我不知道
html
文件中有什么干杯
首先,感谢皮埃特罗指出我的错误
其次,我得到了这个工作,但不是100%,它将所有的文件转换为excel,但我不能给他们他们的原始名称,因为它不做任何事情,也不会产生错误,当我运行它试图保持他们的原始名称
相关问题 更多 >
编程相关推荐