哪个语言都可以,除了字体格式,怎么自动创建表格
仅供参考:
//1.在VC中新建一控制台程序,选支持MFC(当然,你也可以不选择支持MFC的,不过会很麻烦)
//2.按CTRL+W调出MFC ClassWizard,Add Class->From a type library,选择你的word的类型库
// (例如我的是word2003,安装在e盘,我的路径是"e:\edittools\microsoft office\office11\msword.olb"),
// 选择完毕后,在弹出的窗口中选择要让classwizard生成的包装类,在本例中要用到
// _Application,
// Documents,
// _Document,
// Range
// 这四个类,选中他们后按OK
//3.进入你的main函数所在的cpp文件,加入头文件引用
// #include "msword.h" //引用刚才classwizard生成的idispatch包装类
//4.加入代码
// console_word.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "console_word.h"
#include "msword.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
printf(_T("Fatal Error: MFC initialization failed!\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
if (CoInitialize(NULL) != S_OK)
{
AfxMessageBox("初始化COM支持库失败!");
return -1;
}
_Application wordApp;
Documents docs;
_Document doc;
Range aRange;
COleVariant vTrue((short)TRUE), vFalse((short)FALSE), vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CString txt;
wordApp.CreateDispatch("Word.Application",NULL);
wordApp.SetVisible(FALSE);
docs=wordApp.GetDocuments();
doc=docs.Open(COleVariant("c:\\new\\测试.doc"),vFalse,vTrue,vFalse,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt);
aRange=doc.Range(vOpt,vOpt);
txt=aRange.GetText();
AfxMessageBox(txt);//这里GetText得到的就是word文件的纯文本了,你可以将其写到txt文件中
printf("[%s]\n",txt.GetBuffer(txt.GetLength()));//里面的换行不是\r\n而是\r,所以需要输出重定向到文本文件看结果。
aRange.ReleaseDispatch();
doc.Close(vOpt,vOpt,vOpt);
doc.ReleaseDispatch();
docs.ReleaseDispatch();
wordApp.Quit(vOpt,vOpt,vOpt);
wordApp.ReleaseDispatch();
CoUninitialize();
}
return nRetCode;
}
在Word中开始记录宏,手动完成所需功能,结束记录宏,按Alt+F11键,查看刚才记录的宏对应的VBA代码。
[Java]
设置字体样式:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class SetCharacterFormat {
public static void main(String[] args) {
//创建Word文档
Document document = new Document();
Section sec = document.addSection();
//添加段落
Paragraph paragraph = sec.addParagraph();
paragraph.appendText("这是一段样式繁多的文字,我是");
//在段落中添加文字,并返回TextRange对象
TextRange tr = paragraph.appendText("带删除线的文字");
//通过TextRange对象设置文字带删除线
tr.getCharacterFormat().isStrikeout(true);
//设置文字阴影效果
paragraph.appendText(",我是");
tr = paragraph.appendText("带阴影的文字");
tr.getCharacterFormat().isShadow(true);
//设置文字字号
paragraph.appendText(",我是");
tr = paragraph.appendText("加大的文字");
tr.getCharacterFormat().setFontSize(20);
//设置文字颜色
paragraph.appendText(",我是");
tr = paragraph.appendText("红色的文字");
tr.getCharacterFormat().setTextColor(Color.red);
//设置文字斜体加粗
paragraph.appendText(",我是");
tr = paragraph.appendText("斜体加粗的文字");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setItalic(true);
//设置文字带下划线
paragraph.appendText(",我是");
tr = paragraph.appendText("带下划线的文字");
tr.getCharacterFormat().setUnderlineStyle(UnderlineStyle.Single);
//设置文字背景色
paragraph.appendText(",我是");
tr = paragraph.appendText("带背景色的文字");
tr.getCharacterFormat().setTextBackgroundColor(Color.GREEN);
//为文字添加上标
paragraph.appendText(",这里还有一个公式:a");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText(" + b");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText(" = c");
tr = paragraph.appendText("2");
tr.getCharacterFormat().setSubSuperScript(SubSuperScript.Super_Script);
paragraph.appendText("。");
//对整个段落设置为宋体(默认为英文字体)
ParagraphStyle style1 = new ParagraphStyle(document);
style1.setName("style");
style1.getCharacterFormat().setFontName("宋体");
document.getStyles().add(style1);
paragraph.applyStyle(style1.getName());
//保存文档
document.saveToFile("SetCharacterFormat.docx", FileFormat.Docx);
}
}
创建表格:
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class CreateTable {
public static void main(String[] args) {
//创建Word文档
Document document = new Document();
//添加一个section
Section section = document.addSection();
//数据
String[] header = {"姓名", "性别", "部门", "工号"};
String[][] data =
{
new String[]{"Winny", "女", "综合", "0109"},
new String[]{"Lois", "女", "综合", "0111"},
new String[]{"Jois", "男", "技术", "0110"},
new String[]{"Moon", "女", "销售", "0112"},
new String[]{"Vinit", "女", "后勤", "0113"},
};
//添加表格
Table table = section.addTable(true);
//设置表格的行数和列数
table.resetCells(data.length + 1, header.length);
//设置第一行作为表格的表头并添加数据
TableRow row = table.getRows().get(0);
row.isHeader(true);
row.setHeight(20);
row.setHeightType(TableRowHeightType.Exactly);
row.getRowFormat().setBackColor(Color.gray);
for (int i = 0; i < header.length; i++) {
row.getCells().get(i).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
Paragraph p = row.getCells().get(i).addParagraph();
p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
TextRange range1 = p.appendText(header[i]);
range1.getCharacterFormat().setFontName("Arial");
range1.getCharacterFormat().setFontSize(12f);
range1.getCharacterFormat().setBold(true);
}
//添加数据到剩余行
for (int r = 0; r < data.length; r++) {
TableRow dataRow = table.getRows().get(r + 1);
dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Exactly);
dataRow.getRowFormat().setBackColor(Color.white);
for (int c = 0; c < data[r].length; c++) {
dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
range2.getCharacterFormat().setFontName("Arial");
range2.getCharacterFormat().setFontSize(10f);
}
}
//设置单元格背景颜色
for (int j = 1; j < table.getRows().getCount(); j++) {
if (j % 2 == 0) {
TableRow row2 = table.getRows().get(j);
for (int f = 0; f < row2.getCells().getCount(); f++) {
row2.getCells().get(f).getCellFormat().setBackColor(new Color(173, 216, 230));
}
}
}
//保存文档
document.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
}
}
python 修改字体格式应该是 (font.name) https://python-docx.readthedocs.io/en/latest/dev/analysis/features/text/font.html?highlight=font.name#protocol
表格的话好像是 (add_table) https://python-docx.readthedocs.io/en/latest/api/table.html?highlight=table 这个应该是可以和panda一起用的
像这种关于包的问题,我建议你去官方的文档里看一下,它有示例也有说明。