Py学习  »  Python

将Python输出写入xlsx

Shivam • 1 年前 • 307 次点击  

我想对给定路径中可用的所有文件(库)执行相同的函数(给出输出A、B、C、D)。我正在尝试将输出(A、B、C、D)写入xlsx中工作表的四个不同列中。此外,xlsx的图纸名称应与路径中可用的受尊重文件相同。

我编写了以下代码:

def create_xlsx_file(xlsx_name, file_path): 
    
    workbook = xlsxwriter.Workbook(xlsx_name) ### creates a xlsx file
    workbook.close()
    libraries=os.listdir(file_path)
    
    file_path=os.chdir(file_path)
    for library in libraries: ### to create the sheets named same as the library 
        # print(library)
        if library.endswith('.txt'):
            # library=file_path+library
            # print(library)
            main(library, xlsx_name) 

def main(library, xlsx_name): ###library = all files in the given path
    directory=os.chdir(os.getcwd())
    workbook = openpyxl.load_workbook(xlsx_name)
    worksheet = workbook.create_sheet(library, 0)##### creates workshhets named same as library name
    #print('library is: - ',library)
    sheet=workbook[library] ###to create column headers
    sheet.cell(column=1, row=1, value='value_A')
    sheet.cell(column=2, row=1, value='value_B')
    sheet.cell(column=3, row=1, value='value_C')
    sheet.cell(column=4, row=1, value='value_D')
    workbook.save(xlsx_name)
    with open(library, 'r') as library:
        for line in library:

            A=line.split(' ')[0]
            B=line.split(' ')[1]
            C=line.split(' ')[2]
            D=line.split(' ')[3]

            sheet=workbook[library]
            sheet.cell(column=1, row=sheet.max_row+1, value=str(A))
            sheet.cell(column=2, row=sheet.max_row, value=str(B))
            sheet.cell(column=3, row=sheet.max_row, value=str(C))
            sheet.cell(column=4, row=sheet.max_row, value=str(D))
            
    print(f'library  {library} has been written at {os.getcwd()}')
    #time.sleep(1)
    workbook.save(xlsx_name)

这段代码对我来说非常好,但写xlsx文件太慢了,因为我的路径有数百个。txt库和每个库都有数百万行。

我可以将输出(A、B、C、D)保存为。txt格式,然后可以手动编写xlsx文件,但这是非常费力的。

有没有办法加快这一进程?或提供其他任何快速xlsx写入程序? 任何帮助都将不胜感激。 谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/135378
 
307 次点击  
文章 [ 4 ]  |  最新文章 1 年前