【问题标题】:Real-Time Copying and Pasting to Excel实时复制和粘贴到 Excel
【发布时间】:2020-06-23 04:34:17
【问题描述】:

我使用 openpyxl 和 pyperclip 制作了这个简单的程序,它将剪贴板的内容粘贴到 Excel 中的连续行 - 例如,如果我复制单词“hello”,它会将“hello”粘贴到单元格 'A2、A3 , A4...'

但是,我的主要目标是创建一个程序,我可以在其中实时复制文本并将其粘贴到单元格中。例如,我复制文本“你好”,程序将其粘贴到 A2,然后我复制文本“我喜欢 python”并将其粘贴到单元格 A3 等等(然后我按一个键结束程序当我完成时)。

这可能吗?如果是这样,我该怎么做? (我愿意下载新的库等)

import openpyxl
from openpyxl import Workbook
import pyperclip

wb = Workbook()

column = 'A'
row = 0

# grab the active worksheet
ws = wb.active

#loop through excel rows
for x in range(2,6):
   row = x
   cell = column + str(row)
   ws[cell] = pyperclip.paste()
   
# Save the file
wb.save("sample.xlsx")

其他帮助:在这种情况下,我(在您的帮助下)想出了如何做到这一点,如果有人能告诉我如何保存手机号码/密码,我也将不胜感激。因此,例如我运行程序并将相应的文本粘贴到单元格“A1、A2、A3...A14”上。有没有办法将“A14”也保存到内存中,所以下次我运行程序时,它会从 A14 开始并相应地粘贴 -'A14,A15,A16...'

【问题讨论】:

    标签: python excel copy openpyxl pyperclip


    【解决方案1】:
    import openpyxl
    from openpyxl import Workbook
    import pyperclip
    
    
    wb = Workbook()
    
    column = 'A'
    row = 0
    
    # grab the active worksheet
    ws = wb.active
    
    clipboard = ["quote", "random"]
    
    limit = 10
    
    recentcopy = 'a'
    
    x = 1
    while(len(clipboard)<limit):
        recentcopy = pyperclip.paste()
        if(recentcopy != clipboard [x]):
            clipboard.append(pyperclip.paste())
            print(clipboard)
            row = x + 1
            cell = column + str(row)
            ws[cell] = clipboard[x]
            wb.save("heyo.xlsx")
            x = x+1
    

    【讨论】:

    • 这是有效的。如果有人可以回答其他问题,我仍然会很感激。
    【解决方案2】:

    如果您想要一个简单的解决方案来存储您所在的单元格,我建议您将单元格写入文本文件,然后让您的程序在启动时读取该文本文件,以便知道从哪里继续。使用 python 的文本文件教程可以在这里找到:https://www.geeksforgeeks.org/reading-writing-text-files-python/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      相关资源
      最近更新 更多