简体   繁体   English

使用 Python 替换文件中 Excel 表中的文本

[英]Replace the text from an Excel Sheet in a File Using Python

I am very new in Python and I am trying to figure out how to write a code which searches a text file for a string in a cell in Excel sheet and replace that string with a consequtive cell from the same row.我是 Python 的新手,我正在尝试弄清楚如何编写代码,在文本文件中搜索 Excel 工作表中单元格中的字符串,然后用同一行中的后续单元格替换该字符串。

+-----------+------------+--------+
|    A      |    B       |    C   |
+-----------+------------+--------+
|    a      |    a_1     |  a_2   |
|    b      |    b_1     |  b_2   |
|    c      |    c_1     |  c_2   |
+-----------+------------+--------+

In the text_file it's written "This is a_xyz" .在 text_file 中写着"This is a_xyz"

What I try to do is, to create multiple text files like "This is a_1" and "This is a_2" and save them automatically.我尝试做的是创建多个文本文件,如“这是 a_1”“这是 a_2”并自动保存它们。 Of course there will be more rows and columns.当然会有更多的行和列。

Tricky thing for me is, the string is "a" in Excel sheet, but it needs to replace the whole "a_xyz" in which "a" is present.对我来说棘手的是,Excel 表中的字符串是“a” ,但它需要替换存在“a”的整个“a_xyz”

Any suggestions?有什么建议么? Thanks in advance.提前致谢。

you'd probably want to use something like this library https://openpyxl.readthedocs.io/en/stable/ to open up the file.你可能想使用类似这个库https://openpyxl.readthedocs.io/en/stable/的东西来打开文件。 as far as I'm aware excel's format is not easily translated to text (try opening it in notepad and see for yourself:)).据我所知,excel的格式不容易翻译成文本(尝试在记事本中打开它并亲自查看:))。 it's basically binary data.它基本上是二进制数据。 Go and read the docs at the link I posted and you should be able to figure out how to iterate over each row. Go 并阅读我发布的链接中的文档,您应该能够弄清楚如何遍历每一行。 as a first step try saving the exactly same excel file by going through them row by row.作为第一步,尝试通过逐行浏览它们来保存完全相同的 excel 文件。 once you've done that, just edit the place where you're doing the save so that it swaps those columns.完成后,只需编辑要保存的位置,以便交换这些列。 cheers干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel 使用 python 和 panda 将数据表转换为文本文件 - Excel Data sheet to a text file using python and panda 使用python将文本文件(对象)作为.txt插入到Excel工作表的单元格中 - Insert a text file (object) as .txt into a cell of excel sheet using python 如何使用 Python 从多个文本文件中提取数据到 Excel? (每张纸一个文件的数据) - How do I extract data from multiple text files to Excel using Python? (One file's data per sheet) 使用 Python 如何从 Excel 文件中获取输入、定义函数并在该 Excel 文件的新工作表中生成输出? - Using Python how to take input from an Excel file , define a function and generate ouput in a new sheet of that Excel file? 使用 python 将列从一个 excel 文件复制到另一个 excel 文件表 - Copy columns from one excel file to another excel file sheet using python 使用 Excel 工作表中的数据在 python 中绘制图形 - Using data from an excel sheet to graph in python 使用python从Excel工作表中提取公式 - Extracting formulas from excel sheet using python 使用 python 从 excel 表中删除行 - delete the rows from excel sheet using python 使用python从excel表中删除值 - Remove values from excel sheet using python 使用python在现有Excel文件中的不同工作表中的相同Excel中的新工作表中合并结果摘要 - Consolidating Result Summary in new sheet in same excel from different sheets on a existing excel file using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM