目标: 使用python(3.7)在(.xlsx)文档中附加一行。
我希望附加到Document.xlsx的最后一行。 (单元格分隔符:“ |”):
(Last_Row): [Tom] | [Tablet] | [003332] | [Returned] | [PSEA]
Document.xlsx:
[] [ A ] | [ B ] | [ C ] | [ D ] | [ E ]
1: [FIID:] | [Device:] | [Gov_ID:] | [ Status ] | [Project:]
2: [John ] | [Laptop ] | [002435 ] | [Resigned] | [ AWSD ]
...
+4,000 rows
到目前为止,我正在从(.csv)文档中追加内容。
Pulling_Data_CSV.py
import csv
df = pd.read_csv(r"C:\...\Test_Pandas.csv")
with open(r"C:\...\Test_Pandas.csv", "a",newline="") as
csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Tom", "Laptop", "003332", "Returned", "AWSD"])
编辑(.xlsx)文档相当于什么?
如果不可能,我将如何将(.xlsx)转换为(.csv),追加一行,然后将其转换回(.xlsx)文档?
答案 0 :(得分:1)
下面是使用openpyxl的示例:
import openpyxl
filename = 'C:\\temp\\spreadsheet.xlsx'
wb = openpyxl.load_workbook(filename=filename)
sheet = wb['Sheet1']
new_row = ['column1', 'column2', 'column3']
sheet.append(new_row)
wb.save(filename)
答案 1 :(得分:0)
xlsx是基于xml的复杂容器格式。您可以自己解析,但可能想检出像openpyxl这样的模块,然后从那里开始。