English 中文(简体)
每一页的书写方式,而不删除以前的数据
原标题:Loop for writing in every sheet without erasing the previous data

我收到了一份<条码>定购单,我已将其转至带有多个钥匙的新的外部文件。 每一关键部分对应的是单壳档案中的表格。 我想用一个声音,让我能够在我档案中的所有表格的A1职位上写字。

  • The code below writes my ordereddict in a new excel file:

# Importing modules
import openpyxl as op
import pandas as pd
import numpy as np
import xlsxwriter
from openpyxl import Workbook, load_workbook

# Defining my file
my_file = r machukovichDesktopstack.xlsx 

# Loading the file into a dictionary of Dataframes
dfs_my_file = pd.read_excel(my_file, sheet_name=None, skiprows=2)

# The path of the new file I wish to write on
my_new_path = r machukovichDesktop
ew.xlsx 

# At this point I have made a few modifications in dfs_my_file which are non important to the core of this question.

# Create a Pandas Excel writer using XlsxWriter as the engine.

with pd.ExcelWriter(my_new_path, engine="xlsxwriter") as writer:
    for sheet_name, df in dfs_my_file.items():
            df.to_excel(writer, sheet_name=sheet_name, startrow=6, index=False)

# Close the Pandas Excel writer and output the Excel file.
writer.close()    
writer.save()
  • My dataset for dfs_my_file (the ordered dictionary):

{ Sheet_1 :     ID      Name  Surname  Grade
 0  104  Eleanor     Rigby      6
 1  168  Barbara       Ann      8
 2  450    Polly   Cracker      7
 3   90   Little       Joe     10,
  Sheet_2 :     ID       Name   Surname  Grade
 0  106       Lucy       Sky      8
 1  128    Delilah  Gonzalez      5
 2  100  Christina   Rodwell      3
 3   40      Ziggy  Stardust      7,
  Sheet_3 :     ID   Name   Surname  Grade
 0   22   Lucy  Diamonds      9
 1   50  Grace     Kelly      7
 2  105    Uma   Thurman      7
 3   29   Lola      King      3}
  • I tried with the code below:

# Defining workbook and worksheet

workbook = xlsxwriter.workbook
worksheet = writer.sheets[sheet_name]

# I tried with this iteration

with pd.ExcelWriter(my_new_path, engine="xlsxwriter") as writer:
        for sheet_name, df in dfs_my_file():
            worksheet.write( A1 ,  RANDOM TEXT 1 )
            worksheet.write( A2 ,  RANDOM TEXT 2 )
  • But I end up with the error below, without understanding the origin of it.

KeyError                                  Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_18328/702301548.py in <module>
      1 workbook = xlsxwriter.workbook
----> 2 worksheet = writer.sheets[sheet_name]
      3 
      4 with pd.ExcelWriter(my_new_path, engine="xlsxwriter") as writer:
      5         for sheet_name, df in dfs_my_file():

KeyError:  Sheet_1 

是否有任何人曾经历过这一错误? 你们能否帮助我对我的 ex子档案进行解释? 在这方面。

问题回答

IIUC:

def write_sheets(data: dict) -> None:
    with pd.ExcelWriter("/machukovich/Desktop/new.xlsx", engine="xlsxwriter") as writer:
        [df.to_excel(writer, sheet_name=sheet_name, index=False) for sheet_name, df in data.items()]


write_sheets(data=dfs_my_file)




相关问题
re-arrange data by pairs recursively

I have dataframe contains ACQ/REL pair recusively as below: import pandas as pd data = [ [ 2023-06-05 16:51:27.561 , ACQ , location ], [ 2023-06-05 16:51:27.564 , ACQ , location ], [ ...

Filling NAN values in Pandas by using previous values

I have a Pandas DataFrame in the following format. I am trying to fill the NaN value by using the most recent non-NaN value and adding one second to the time value. For example, in this case, the ...

Python/Pandas convert string to time only

I have the following Pandas dataframe in Python 2.7. import pandas as pd trial_num = [1,2,3,4,5] sail_rem_time = [ 11:33:11 , 16:29:05 , 09:37:56 , 21:43:31 , 17:42:06 ] dfc = pd.DataFrame(zip(*[...

Pretty-print an entire Pandas Series / DataFrame

I work with Series and DataFrames on the terminal a lot. The default __repr__ for a Series returns a reduced sample, with some head and tail values, but the rest missing. Is there a builtin way to ...

How to invert the x or y axis

I have a scatter plot graph with a bunch of random x, y coordinates. Currently the Y-Axis starts at 0 and goes up to the max value. I would like the Y-Axis to start at the max value and go up to 0. ...

热门标签