繁体   English   中英

如何自动从 excel 复制数据并粘贴到单词中?

[英]How can I automaticallly copy data from excel and paste into a word?

我尝试将 docxtpl 与 Python 一起使用,但返回非常难看,几乎不可读。 我尝试使用 Dataframe,列表,......但我没有一张干净的桌子。 有谁知道如何用 Python 制作它? 还是使用 VBA 更简单?

(而且 docx 不允许我在 Word 内部添加我想要的表格。)使用 Dataframe 表格是 trounce。 并且使用列表,列不适合....

非常感谢

from docxtpl import DocxTemplate
doc = DocxTemplate(fichier_test)
context = {para_multiple[i]: liste_dataframe[i] for i in range(len(para_multiple))}
doc.render(context)
doc.save(file_location)

其中 para_multiple 是包含 .doc 和 liste_dataframe 中所有标签的列表,dataframe 的列表包含我在文档中需要的数据。

(这是我现在得到的,我不知道如何正确显示它)我需要删除表格和索引

https://i.stack.imgur.com/r3CQJ.png

在 Windows 中,如果您尝试从 Excel 复制的数据在命名范围内,您可以使用以下位将其粘贴到 Word 文档中:

from win32com import client

excel = client.Dispatch("Excel.Application")
word = client.Dispatch("Word.Application")

doc = word.Documents.Open("C:/word_file.docx")
book = excel.Workbooks.Open("C:/excel_file.xlsx")

sheet = book.Worksheets(1) #depends on which sheet your named range is located
sheet.Range(NAMED_RANGE_OF_YOUR_TABLE_IN_EXCEL).Copy() 
  
target = doc.Range()
findtext = "WHERE_I_WANT_TO_PASTE_MY_TABLE_IN_WORD" #use a placeholder in your word document where you want the table to appear

if target.Find.Execute(FindText=findtext):
   table_range = doc.Range(Start = target.start, End=target.end)
   table_range.PasteExcelTable(False, False, False)

它将保留工作簿中的格式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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