简体   繁体   中英

Python: Updating Exported Data to Excel (OpenPyXl)

Nested_Array = [['GOOGL', 9822.6], ['FB', 98.25], ['SPY', 1291]]

now = datetime.datetime.now()
Current_Date = (now.strftime('%Y-%m-%d'))

row_start = 1
col_start = 1

ws4.cell(row=row_start, column=col_start + 1).value = Current_Date

for ticker, profit in (Nested_Array):
    ws4.cell(row=row_start + 1, column=col_start).value = ticker
    ws4.cell(row=row_start + 1, column=col_start + 1).value = profit
    row_start += 1

The screenshot below shows what it exports. However, when I run the program, I'd like it to save the previous days data and then move to a different column for the next day.

If that's possible, I'd like the program to see if it's still the same day when I run it, if it is, it should just update the cells of the days column it's in rather than moving to a new column. Although, if it's not the same day, I'd like it to move to the next day.

Any feedback helps!

This is what it exports currently:

在此处输入图片说明

This is what I'd like it to do if it's a new day:

在此处输入图片说明

In openpyxl you should generally avoid creating your own counters because it provides useful methods that get this right.

You can probably do something like this:

col_idx = ws.max_col + 1
ws.cell(1, col_idx) = CurrentDate

for stock, row in zip(NestedArray, ws.iter_cols(min_col=col_idx, max_col=col_idx, min_row=2):
     row[0].value = stock[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM