如何使用python读取和编辑excel文件
我对python相当陌生,正在尝试做以下工作: 我有一个excel数据表,有两列。第一个包含名称,第二个包含数据。如果我有一个程序名,它需要在电子表格中找到该名称,向右移动一个单元格(下一列)读取该值。在那之后,我有一个小的公式来改变值,然后我想把数据存储在它来自的同一个单元格中 之后,一切又重新开始 现在我知道我需要使用xlsxwriter来完成这项工作,但我无法找到正确的代码。我找到了这个页面: 但这不是我想要的。我不是要你们给我一个密码,但你们能帮我找到正确的方向吗?(事实上,我宁愿自己编写代码,因为这是学习Python的好方法) 提前谢谢 编辑:如何使用python读取和编辑excel文件,python,excel,Python,Excel,我对python相当陌生,正在尝试做以下工作: 我有一个excel数据表,有两列。第一个包含名称,第二个包含数据。如果我有一个程序名,它需要在电子表格中找到该名称,向右移动一个单元格(下一列)读取该值。在那之后,我有一个小的公式来改变值,然后我想把数据存储在它来自的同一个单元格中 之后,一切又重新开始 现在我知道我需要使用xlsxwriter来完成这项工作,但我无法找到正确的代码。我找到了这个页面: 但这不是我想要的。我不是要你们给我一个密码,但你们能帮我找到正确的方向吗?(事实上,我宁愿自己编
我现在明白了。但它不起作用,D的值保持为4。为什么?我想你应该试试熊猫。您将能够读取整个表并对其进行操作。学习如何使用熊猫并不困难,但它非常有用 您将能够执行以下操作:
df = pd.read_excel('filename')
df.loc[df.col1 == 'name', col2] = formula
首先,您读取该文件。然后在第一列中找到一个值,并在第二列中修改此行中的值
UPD:
这是您的案例的解决方案。正如我所说,您可以直接修改dataframe中的值。代码在“Name”列中找到带有“D”的行,并在“value”列中的值上加50
df = pd.DataFrame(
{"Name" :['A' ,'B', 'C', 'D', 'E', 'F'],
"Value" :[1 ,2 ,3 ,4 ,5 ,6]},
)
df.loc[df.Name == 'D', 'Value'] += 50
df
Name Value
0 A 1
1 B 2
2 C 3
3 D 54
4 E 5
5 F 6
df.to_excel('Test.xlsx')
碰巧我用openpyxl写了一个函数
import openpyxl
import os
def load_FromExcel(filepath, total_row = 999999999999):
"""Using module "openpyxl" to load data from excel file(only support '.xlsx' format)
Make sure you already install "openpyxl".
:param filepath:
Source file should be put under the path beneath the file which is calling this method.
Otherwise you'll have to change some code inside.
:return:
list_row is a list consists of list records.
For example, list_row[0] is a list consists of these cell in 1st row.
Therefore, use list_row[0][0] represent the value in 1st column in 1st row(A1).
"""
filename = os.path.join(os.path.dirname(__file__) + "/../" + filepath)
# Use "os.path.dirname(__file__)" to get a relevant path
print(filename)
# Open a workbook by name= filename in READ-ONLY mode
wb = openpyxl.load_workbook(filename, read_only=True)
# Select this worksheet
sheet = wb.get_active_sheet()
sheet_rows = tuple(sheet.rows)
# This is the total amount of rows in the excel file opened
row_count = len(sheet_rows)
print(row_count)
row_count = 0
list_row = []
for row in sheet.rows:
row_count += 1
temp_list = []
for cell in row:
# print(cell.value,end=',')
temp_list.append(cell.value)
list_row.append(temp_list)
if row_count > total_row:
break
print("Loading Complete")
return list_row
在使用代码之前,请务必保持自己的代码整洁。您好,欢迎来到Stack Overflow。我很感激你注意到你没有试图要求别人为你编码。但是,您的问题仍然不符合中的社区指南。堆栈溢出更多用于帮助解决特定的编码问题。一种更有效且被接受的提问方式是自己尝试解决问题,然后如果有任何特定部分你不理解,或者错误你无法解决,那么询问这些问题。这有助于你了解自己能做什么,也有助于我们挑战我们,让我们想出比“看这里”更具创造性的答案。对不起,我希望有人能把我引向正确的方向。就像安德烈在回答关于熊猫的问题时所做的那样,这真的很有帮助。我试了一下你的线路,但是col1出错了。所以我尝试了一些其他的东西(见编辑后的文章),但仍然不起作用哈哈。有时真令人沮丧。
import openpyxl
import os
def load_FromExcel(filepath, total_row = 999999999999):
"""Using module "openpyxl" to load data from excel file(only support '.xlsx' format)
Make sure you already install "openpyxl".
:param filepath:
Source file should be put under the path beneath the file which is calling this method.
Otherwise you'll have to change some code inside.
:return:
list_row is a list consists of list records.
For example, list_row[0] is a list consists of these cell in 1st row.
Therefore, use list_row[0][0] represent the value in 1st column in 1st row(A1).
"""
filename = os.path.join(os.path.dirname(__file__) + "/../" + filepath)
# Use "os.path.dirname(__file__)" to get a relevant path
print(filename)
# Open a workbook by name= filename in READ-ONLY mode
wb = openpyxl.load_workbook(filename, read_only=True)
# Select this worksheet
sheet = wb.get_active_sheet()
sheet_rows = tuple(sheet.rows)
# This is the total amount of rows in the excel file opened
row_count = len(sheet_rows)
print(row_count)
row_count = 0
list_row = []
for row in sheet.rows:
row_count += 1
temp_list = []
for cell in row:
# print(cell.value,end=',')
temp_list.append(cell.value)
list_row.append(temp_list)
if row_count > total_row:
break
print("Loading Complete")
return list_row