如何将多个多行TXT文件转换为Excel-确保每个文件都是其自己的行,然后每条文本都是自己的行? python3

发布于 2025-01-24 15:18:33 字数 1138 浏览 3 评论 0原文

使用openpyxl和路径我的目标是: 创建多个Multiline .TXT文件, 然后将.txt内容插入.xlsx文件,以确保文件1在第1列中,并且每行都有自己的行。

我认为创建一个嵌套列表,然后循环插入文本。我不知道如何确保显示所有嵌套列表字符串。到目前为止,这就是我所拥有的几乎可以完成我想要的东西,但是这只是第一行文本的重复。

from pathlib import Path
import openpyxl
listOfText = []

wb = openpyxl.Workbook() # Create a new workbook to insert the text files
sheet = wb.active

for txtFile in range(5): # create 5 text files
    createTextFile = Path('textFile' + str(txtFile) + '.txt')
    createTextFile.write_text(f'''Hello, this is a multiple line text file.
My Name is x.
This is text file {txtFile}.''')

    readTxtFile = open(createTextFile)
    listOfText.append(readTxtFile.readlines()) # nest the list from each text file into a parent list

textFileList = len(listOfText[txtFile]) # get the number of lines of text from the file. They are all 3 as made above

# Each column displays text from each text file
for row in range(1, txtFile + 1):
    for col in range(1, textFileList + 1):
        sheet.cell(row=row, column=col).value = listOfText[txtFile][0]

wb.save('importedTextFiles.xlsx')

输出为4列/4行。所有这些都说相同的“你好,这是一个多行文本文件。”

感谢此帮助!

Using openpyxl and Path I aim to:
Create multiple multiline .txt files,
then insert .txt content into a .xlsx file ensuring file 1 is in column 1 and each line has its own row.

I thought to create a nested list then loop through it to insert the text. I cannot figure how to ensure that all the nested list string is displayed. This is what I have so far which nearly does what I want however it's just a repeat of the first line of text.

from pathlib import Path
import openpyxl
listOfText = []

wb = openpyxl.Workbook() # Create a new workbook to insert the text files
sheet = wb.active

for txtFile in range(5): # create 5 text files
    createTextFile = Path('textFile' + str(txtFile) + '.txt')
    createTextFile.write_text(f'''Hello, this is a multiple line text file.
My Name is x.
This is text file {txtFile}.''')

    readTxtFile = open(createTextFile)
    listOfText.append(readTxtFile.readlines()) # nest the list from each text file into a parent list

textFileList = len(listOfText[txtFile]) # get the number of lines of text from the file. They are all 3 as made above

# Each column displays text from each text file
for row in range(1, txtFile + 1):
    for col in range(1, textFileList + 1):
        sheet.cell(row=row, column=col).value = listOfText[txtFile][0]

wb.save('importedTextFiles.xlsx')

The output is 4 columns/4 rows. All of which say the same 'Hello, this is a multiple line text file.'

Appreciate any help with this!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2025-01-31 15:18:33

问题是在写作时在for循环中,更改行sheet.cell(row = row,column = col).value = listOfText [txtfile] [0] to exphing.cell(cell.cell)(行= col,column = row).value = listOfText [row-1] [col-1],它将工作

The problem is in the for loop while writing, change the line sheet.cell(row=row, column=col).value = listOfText[txtFile][0] to sheet.cell(row=col, column=row).value = listOfText[row-1][col-1] and it will work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文