python - 如何将转换后的文件写入另一个目录
问题描述
我是 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
期望的结果:程序将“Morning”文件夹中的每个 html 文件从 html 转换为 excel,并在“Evening”文件夹中写入/保存新的 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)
解决方案
首先,感谢 Pietro 指出我的错误。
其次,我得到了这个工作但不是 100%,它将所有文件转换为 excel,但我不能给它们它们的原始名称,因为当我运行它试图保留它们的原始名称时它什么都不做并且不会产生错误。
from html2excel import ExcelParser
import os
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
i=0
for file in os.listdir(inputFolder):
if file.endswith(".html"):
i=i+1
suffix=".xlsx"
#name = str(file) + "\b\b\b\b" + "xlsx"
#out= r'C:/Users/Ahmed_Abdelmuniem/Desktop/Evening/%s' % (name)
out= r'C:/users/Ahmed_Abdelmuniem/Desktop/Evening/%d%s' % (i,suffix)
print(str(out))
parser = ExcelParser(file)
parser.to_excel(out)