【问题标题】:Copying from xlsx to an specific sheet in another xlsx从 xlsx 复制到另一个 xlsx 中的特定工作表
【发布时间】:2017-07-03 05:09:58
【问题描述】:

我需要一些关于 python 的帮助。基本上我有 2 个文件(对于这个例子,文件 1 和文件 2)。 File1 里面有几张纸,file2 只是一张纸。因此,在 file2 中进行了一些工作之后,现在我有了我需要的 DataFrame。我需要将此 DataFrame 粘贴到 file1 的一个特定工作表中。

File1

  A       B       C       D         E          F         G
<data>  <data>  <data>  <data>  <formula>  <formula>  <formula>
<data>  <data>  <data>  <data>  <formula>  <formula>  <formula>
<data>  <data>  <data>  <data>  <formula>  <formula>  <formula>
<data>  <data>  <data>  <data>  <formula>  <formula>  <formula>
<data>  <data>  <data>  <data>  <formula>  <formula>  <formula>
<data>  <data>  <data>  <data>  <formula>  <formula>  <formula>

File2

    A          B          C         D         
<Newdata>  <Newdata>  <Newdata>  <Newdata>
<Newdata>  <Newdata>  <Newdata>  <Newdata>
<Newdata>  <Newdata>  <Newdata>  <Newdata>
<Newdata>  <Newdata>  <Newdata>  <Newdata>
<Newdata>  <Newdata>  <Newdata>  <Newdata>
<Newdata>  <Newdata>  <Newdata>  <Newdata>

So now i need to update the file one with the new Data.

File1

    A          B          C         D           E          F         G         
<Newdata>  <Newdata>  <Newdata>  <Newdata>  <formula>  <formula>  <formula>
<Newdata>  <Newdata>  <Newdata>  <Newdata>  <formula>  <formula>  <formula>
<Newdata>  <Newdata>  <Newdata>  <Newdata>  <formula>  <formula>  <formula>
<Newdata>  <Newdata>  <Newdata>  <Newdata>  <formula>  <formula>  <formula>
<Newdata>  <Newdata>  <Newdata>  <Newdata>  <formula>  <formula>  <formula>
<Newdata>  <Newdata>  <Newdata>  <Newdata>  <formula>  <formula>  <formula>

因此,E、F 和 G 列有一些公式,这些公式由 A、B、C、D 列中的数据更新。

我尝试了不同的选项来做到这一点。连接两个文件并显示我尝试的列,加载两个文件并使用新信息创建一个新文件...主要问题是在文件 1 中我有几张表,我需要保留,因为列 E、F和 G(有公式的那个),将更新其他工作表。

所以,如果有人帮我解决这个问题,请。谢谢,我将不胜感激

【问题讨论】:

  • 使用openpyxl。它将能够处理.xlsx 文件中所需的所有内容。如果两个文件都使用xlsx,请告诉我,我会尽我所能提供帮助。刚刚注意到它是一个xlsb 文件。不知道那些:(
  • @Sid .xlsb 是一个很大的 xlsx,我可以在两个文件中使用 xlsx,你能帮我吗
  • 嗯..我似乎无法将数据保留在 E、F 和 G 行中。试图找出原因。如果你愿意,我可以将代码发布到现在。

标签: python excel xlsx copying


【解决方案1】:

毫无疑问,有更好的方法可以做到这一点,但我可以这样做:

from openpyxl import load_workbook
import os

os.chdir("Directory Path here")
wb = load_workbook('file.xlsx')
ws = wb.active
#or use the below to pick sheet as by name
# ws = wb.get_sheet_by_name
inde = []
val = []
for col in ws.iter_cols():
    for cell in col:
        h = cell.coordinate
        inde.append(h)
        v = cell.value
        val.append(v)
diction = dict(zip(inde,val))

wb1=load_workbook('file1.xlsx')
ws1 = wb1.active

for i in diction.keys():
    ws1[i] = diction[i]
    wb1.save('file1.xlsx')

【讨论】:

  • 谢谢 Sid,让我试试这个解决方案。
  • 早上好。抱歉很久才回答。不,没有工作。我收到此错误。 for col in ws.iter_cols(): AttributeError: 'Worksheet' object has no attribute 'iter_cols'
  • 奇怪,我刚刚又试了一次,效果很好。仔细检查你最后的代码?
  • 同样的错误。你用的是哪个版本的openpyxl
  • 我是最新的。尝试更新 openpyxl。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多