【问题标题】:How to import data from .txt file to a specifc excel sheet with Python?如何使用 Python 将 .txt 文件中的数据导入特定的 Excel 工作表?
【发布时间】:2020-06-29 16:22:36
【问题描述】:

我正在尝试自动化一个基本上将文本文件中的值读入某些 Excel 单元格的过程。我在 excel 中有一个模板,它将以特定名称从各种工作表中读取数据。例如,模板将从“视频分数”中读取数据。视频乐谱是一个 .txt 文件,我将其复制并粘贴到 Excel 中。每个项目中使用了 5 个不同的文本文件,因此在一段时间后以及有很多项目要完成时,它会变得乏味。

如何将这些 .txt 文件导入或复制并粘贴到 excel 到指定的工作表中?我一直在这个项目的其他部分使用 openpyxl,但如果使用 openpxl 无法完成,我愿意使用另一个库。

我也尝试过打开和读取文件,但我也不知道如何做我想做的事。我找到了我需要的所有文件的列表,只需将它们导入 excel 即可。

提前感谢任何提供帮助的人。

【问题讨论】:

  • 所以,您的要求是将多个文件读入同一张 Excel 表格中......文件中有分隔符吗?
  • 是的,.txt 文件是自动创建的。我可以在 for 循环或任何其他循环中一一完成它们,这没问题。我只需要找到一种使用 Python 将 .txt 文件读入新 Excel 工作表的方法。一旦我弄清楚了,我可以对其他文件重复它。

标签: python openpyxl


【解决方案1】:

熊猫可以解决问题!

方法: 有一张包含文件路径、分隔符、相应的目标工作表名称

的工作表

现在使用 pandas 读取此 excel 表并遍历每个文件详细信息的每一行,读取数据,将其写入同一工作簿的新 excel 表。

import pandas as pd

file_details_path = r"/Users/path for xl sheet/file details/File2XlDetails.xlsx"
target_sheet_path = r"/Users/path to target xl sheet/File samples/FiletoXl.xlsx"

# create a writer to save the file content in excel
writer = pd.ExcelWriter(target_sheet_path, engine='xlsxwriter')


file_details = pd.read_excel(file_details_path,
                             dtype = str,
                             index_col = False
                             )


def write_to_excel(file, trg_sheet_name):
    # writes it to excel
    file.to_excel(writer,
                  sheet_name = trg_sheet_name,
                  index = False,
                  )

# loop through each file record
for index, file_dtl in file_details.iterrows():

    # you can print and check the row content for reference
    print(file_dtl['File_path'])
    print(file_dtl['Separator'])
    print(file_dtl['Target_sheet_name'])

    # reads file
    file = pd.read_csv(file_dtl['File_path'],
                       sep = file_dtl['Separator'],
                       dtype = str,
                       index_col = False,
                       )
    write_to_excel(file, file_dtl['Target_sheet_name'])

writer.save()

希望这会有所帮助!如果您遇到任何问题,请告诉我...

【讨论】:

  • 糟糕!我只是发现它不是写入多张纸,而是总是写入同一张纸....我现在已经更新了我的代码
【解决方案2】:

首先,将TXT文件导入python中的列表,我想TXT文件是这样的

1

2

3

4

....

with open(path_txt, "r") as e:
    list1 = [i for i in e]

然后,我们将列表的值粘贴到您需要的工作表上

from openpyxl import load_workbook

wb = load_workbook(path_xlsx)
ws = wb[sheet_name]
ws["A1"] = "values" #just a header
row = 2 #represent the 2 row of the sheet
column = 1 #represent the column "A" of the sheet

for i in list1:
    ws.cell(row=row, column=column).value = i #getting the current cell, and writing the value of the list
    row += 1 #just setting the current to the next

wb.save(path_xlsx)

希望这对你有用。

【讨论】:

  • 嘿!感谢您的回答。但是,我收到以下错误,我不知道为什么。 AttributeError: 'MergedCell' 对象属性 'value' 是只读的
  • 您是否要编写一个合并的单元格,但不是最左边的单元格?例如,如果我已合并单元格 A1:A2,而我正在尝试编写 A2
  • 我刷新了我的程序,它似乎可以自行修复.. 很奇怪。但是,我的代码中出现了一个新错误。 AttributeError:“NoneType”对象没有“读取”属性。当我尝试保存文件时,会出现这种情况。
  • 很奇怪,这通常发生在调用函数或类方法并且它不返回任何内容或返回 None 时。可以添加完整的错误日志吗?
  • 抱歉回复晚了!因此,无论出于何种原因,它只是将 A、B、C、D、E、F、G、H、I、K、L 行写入 A1。我要写入的文件是 filebin.net/lz2ws3avz34bgy3n
猜你喜欢
  • 2019-12-02
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 2022-09-27
  • 1970-01-01
  • 2021-12-02
  • 2021-05-11
  • 1970-01-01
相关资源
最近更新 更多